<?php
/**
 * 春哥团队（www.cgtblog.com） - 春哥多端多商户万能DIY商城系统
 * Copyright © 2024 春哥技术团队 保留所有权利
 * =========================================================

 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

namespace app\controller;
use think\facade\Db;
class ApiShop extends ApiCommon{
	public function getprolist(){
        $mendian_id = input('param.mendian_id/d',0);
		$where = [];
		$where[] = ['aid','=',aid];
		$where[] = ['ischecked','=',1];
		if(isdouyin == 1){
			$where[] = ['douyin_product_id','<>',''];
		}else{
			$where[] = ['douyin_product_id','=',''];
		}
        if(getcustom('product_bind_mendian')){
            if($mendian_id>0){
                $where[] = Db::raw("find_in_set({$mendian_id},`bind_mendian_ids`) OR find_in_set('-1',`bind_mendian_ids`) OR ISNULL(bind_mendian_ids)");
            }
        }
		//$where[] = ['status','=',1];
		$nowtime = time();
		$nowhm = date('H:i');
		$where[] = Db::raw("`status`=1 or (`status`=2 and unix_timestamp(start_time)<=$nowtime and unix_timestamp(end_time)>=$nowtime) or (`status`=3 and ((start_hours<end_hours and start_hours<='$nowhm' and end_hours>='$nowhm') or (start_hours>=end_hours and (start_hours<='$nowhm' or end_hours>='$nowhm'))) )");
		
		if(input('param.field') && input('param.order')){
			$order = input('param.field').' '.input('param.order').',sort desc,id desc';
		}else{
			$order = 'sort desc,id desc';
		}
		if(input('param.bid')){
			$where[] = ['bid','=',input('param.bid/d')];
		}else{
			$business_sysset = Db::name('business_sysset')->where('aid',aid)->find();
			if(!$business_sysset || $business_sysset['status']==0 || $business_sysset['product_isshow']==0){
				if(!input('param.cpid')){
					$where[] = ['bid','=',0];
				}
			}else{
				if(getcustom('prolist_showjuli') && input('param.cid')){
					$latitude = input('param.latitude');
					$longitude = input('param.longitude');
					if($longitude && $latitude){
						$border = Db::raw("({$longitude}-longitude)*({$longitude}-longitude) + ({$latitude}-latitude)*({$latitude}-latitude) ");
						$blist = Db::name('business')->where('aid',aid)->where('status',1)->where("longitude!='' and latitude!=''")->field('id,longitude,latitude')->order($border)->select()->toArray();
						$bids = [];
						$bjuli = [];
						$b0juli = getdistance($longitude,$latitude,$this->sysset['longitude'],$this->sysset['latitude'],2);
						foreach($blist as $binfo){
							$juli = getdistance($longitude,$latitude,$binfo['longitude'],$binfo['latitude'],2);
							if($juli > $b0juli && !in_array('0',$bids)){
								$bids[] = '0';
								$bjuli['0'] = ''.$b0juli.'km';
							}
							$bids[] = $binfo['id'];
							$bjuli[''.$binfo['id']] = ''.$juli.'km';
						}
						if(!input('param.field') || input('param.field') == 'sort'){
							$order = Db::raw('field(bid,'.implode(',',$bids).'),sort desc,id desc');
						}
					}
				}
			}
		}
		//分类 
		if(input('param.cid')){
			$cid = input('post.cid') ? input('post.cid/d') : input('param.cid/d');
            $where2 = "find_in_set('-1',showtj)";
            if($this->member){
                $where2 .= " or find_in_set('".$this->member['levelid']."',showtj)";
                if($this->member['subscribe']==1){
                    $where2 .= " or find_in_set('0',showtj)";
                }
            }
            $tjwhere[] = Db::raw($where2);
			//子分类
			$clist = Db::name('shop_category')->where($tjwhere)->where('aid',aid)->where('pid',$cid)->column('id');
			if($clist){
				$clist2 = Db::name('shop_category')->where($tjwhere)->where('aid',aid)->where('pid','in',$clist)->column('id');
				$cCate = array_merge($clist, $clist2, [$cid]);
				if($cCate){
					$whereCid = [];
					foreach($cCate as $k => $c2){
						$whereCid[] = "find_in_set({$c2},cid)";
					}
                    $where[] = Db::raw(implode(' or ',$whereCid));
				}
			} else {
                $where[] = Db::raw("find_in_set(".$cid.",cid)");
            }
		} else {
            if(getcustom('product_cat_showtj')) {
                $where2 = "find_in_set('-1',showtj)";
                if($this->member){
                    $where2 .= " or find_in_set('".$this->member['levelid']."',showtj)";
                    if($this->member['subscribe']==1){
                        $where2 .= " or find_in_set('0',showtj)";
                    }
                }
                $tjwhere[] = Db::raw($where2);
                $clist = Db::name('shop_category')->where($tjwhere)->where('aid',aid)->column('id');
                if($clist){
                    $whereCid = [];
                    foreach($clist as $k => $c2){
                        $whereCid[] = "find_in_set({$c2},cid)";
                    }
                    $where[] = Db::raw(implode(' or ',$whereCid));
                }
            }
        }
		
		//商家的商品分类 
		if(input('param.cid2')){
			$cid2 = input('post.cid2') ? input('post.cid2/d') : input('param.cid2/d');
			//子分类
			$clist = Db::name('shop_category2')->where('aid',aid)->where('pid',$cid2)->column('id');
			if($clist){
				$clist2 = Db::name('shop_category2')->where('aid',aid)->where('pid','in',$clist)->column('id');
				$cCate = array_merge($clist, $clist2, [$cid2]);
				if($cCate){
					$whereCid = [];
					foreach($cCate as $k => $c2){
						$whereCid[] = "find_in_set({$c2},cid2)";
					}
                    $where[] = Db::raw(implode(' or ',$whereCid));
				}
			} else {
                $where[] = Db::raw("find_in_set(".$cid2.",cid2)");
            }
		}
		if(input('param.gid')) $where[] = Db::raw("find_in_set(".intval(input('param.gid')).",gid)");
		
		
		if(input('param.proparams')){
			$proparams = input('param.proparams');
			$whereparam = [];
			foreach($proparams as $paramkey=>$paramval){
				if(!$paramval) continue;
				$whereparam[] = "paramdata like '%".'"'.$paramkey.'":"'.$paramval.'"'."%'";
			}
			if($whereparam){
				$where[] = Db::raw(implode(' and ',$whereparam));
			}
		}

		if(input('param.keyword')){
            $searchField = 'name';
            if(getcustom('product_keyword')){
                $searchField = "name|keyword";
            }
			$where[] = ["{$searchField}",'like','%'.input('param.keyword').'%'];
		}
		
		$where2 = "find_in_set('-1',showtj)";
		if($this->member){
			$where2 .= " or find_in_set('".$this->member['levelid']."',showtj)";
			if($this->member['subscribe']==1){
				$where2 .= " or find_in_set('0',showtj)";
			}
		}else{
            $where2 .= " or find_in_set('-2',showtj)";
        }
		$where[] = Db::raw($where2);

		//优惠券可用商品列表
		$cpid = input('param.cpid/d');
		if($cpid > 0){
			$coupon = Db::name('coupon')->where('id',$cpid)->find();
			if($coupon['bid'] == 0 && $coupon['canused_bids'] == 'all'){
			
			}elseif($coupon['bid'] == 0 && $coupon['canused_bids']){
				$where[] = ['bid','in',$coupon['canused_bids']];
			}elseif($coupon['bid'] == 0 && $coupon['canused_bcids']){
				$canused_bids = [];
				foreach(explode(',',$coupon['canused_bcids']) as $bcid){
					$thisbids = Db::name('business')->where('aid',aid)->whereRaw('find_in_set('.$bcid.',cid)')->column('id');
					if($thisbids) $canused_bids = array_merge($canused_bids,$thisbids);
				}
				$where[] = ['bid','in',$canused_bids];
			}else{
				$where[] = ['bid','=',$coupon['bid']];
			}
			if($coupon['fwtype']==1){ //指定类目
				$categoryids = explode(',',$coupon['categoryids']);
				$clist = Db::name('shop_category')->where('pid','in',$categoryids)->select()->toArray();
				foreach($clist as $kc=>$vc){
					$categoryids[] = $vc['id'];
					$cate2 = Db::name('shop_category')->where('pid',$vc['id'])->find();
					if($cate2) $categoryids[] = $cate2['id'];
				}
				$whereCid = [];
				foreach($categoryids as $k => $c2){
					$whereCid[] = "find_in_set({$c2},cid)";
				}
				$where[] = Db::raw(implode(' or ',$whereCid));
			}
			if($coupon['fwtype']==2){ //指定商品
				$where[] = ['id','in',$coupon['productids']];
			}
		}
		$pernum = 10;
		$pagenum = input('post.pagenum');
		if(!$pagenum) $pagenum = 1;
        $field = 'id,bid,pic,name,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint,price_type,stock,sellpoint,product_type,guigedata';
        if(getcustom('plug_tengrui')) {
            $field .= ',house_status,group_status,group_ids,is_rzh,relation_type';
        }
        if(getcustom('shop_other_infor')) {
            $field .= ',xunjia_text';
        }
        if(getcustom('product_xunjia_btn')) {
            $field .= ',xunjia_text,show_xunjia_btn,xunjia_btn_bgcolor,xunjia_btn_color,xunjia_btn_url';
        }
        if(getcustom('product_unit')) {
            $field .= ',product_unit';
        }
        if(getcustom('product_cost_show')) {
            $field .= ',cost_price';
        }

        if(getcustom('show_location')){
            //定位模式
            $sysset = Db::name('admin_set')->where('aid',aid)->field('id,mode,loc_range_type,loc_range,loc_area_type')->find();
            if($sysset['mode']==2){
                $area = input('param.area');
                $longitude = input('param.longitude');
                $latitude = input('param.latitude');
                $b_where = [];
                $b_where[] = ['aid','=',aid];
                $b_where[] = ['status','=',1];
                $b_where[] = ['is_open','=',1];
                if($sysset['loc_range_type']==1){
                    if($latitude && $longitude){
                        $b_distance = $sysset['loc_range']?$sysset['loc_range']*1000:0;
                        $b_having = "round(6378.138*2*asin(sqrt(pow(sin( ({$latitude}*pi()/180-latitude*pi()/180)/2),2)+cos({$latitude}*pi()/180)*cos(latitude*pi()/180)* pow(sin( ({$longitude}*pi()/180-longitude*pi()/180)/2),2)))*1000) <={$b_distance}";
                        $limitBusiness = Db::name('business')->having($b_having)->where($b_where)->select()->toArray();
                        if($limitBusiness){
                            $limitBids = array_column($limitBusiness,'id');
                            $limitBids[] = 0;//追加平台0
                            $where[] = ['bid','in',$limitBids];
                        }else{
                            $where[] = ['bid','=',0];//仅显示平台商品
                        }
                    }
                }else{
                    //同城
                    if($area){
                        //取省或者市
                        $areaArr = explode(',',$area);
                        $areaKey = min(count($areaArr)-1,1);//最小范围取到市
                        $areaName = $areaArr[$areaKey];
                        $b_where[] = ['city','=',$areaName];
                        $limitBids = Db::name('business')->where($b_where)->column('id');
                        if($limitBids){
                            $limitBids[] = 0;
                            $where[] = ['bid','in',$limitBids];
                        }else{
                            $where[] = ['bid','=',0];//仅显示平台商品
                        }
                    }
                }
            }
            //定位模式
        }
		if(getcustom('order_add_mobile')){
			if(input('param.order_add_mobile') == 1){
				$where[] = ['bid','=',0];//手机端订单录入仅显示平台商品
			}
			
		}

		$datalist = Db::name('shop_product')->field($field)->where($where)->page($pagenum,$pernum)->order($order)->select()->toArray();
//		$sql = Db::getlastsql();dd($sql);
		if(!$datalist) $datalist = [];

		foreach($datalist as $k=>$v){
			if(getcustom('product_wholesale') && $v['product_type'] == 4){
				$guigedata = json_decode($v['guigedata'],true);
				$datalist[$k]['gg_num'] =  count($guigedata);
			}
		}

		if(getcustom('prolist_showjuli') && input('param.cid') && $bjuli){
			foreach($datalist as $k=>$v){
				$datalist[$k]['juli'] = $bjuli[''.$v['bid']];
			}
		}
        if(getcustom('plug_tengrui')) {
            if($datalist){
                $tr_check = new \app\common\TengRuiCheck();
                foreach($datalist as $dk=>$dv){
                    //判断是否是否符合会员认证、会员关系、一户、用户组，不符合则直接去掉
                    $check_product = $tr_check->check_product($this->member,$dv,1);
                    if($check_product && $check_product['status'] == 0 ){
                        unset($datalist[$dk]);
                    }
                }
                unset($dv);
                $len = count($datalist);
                if($len<10){
                    //重置索引,防止上方去掉的数据产生空缺
                    $datalist=array_values($datalist);
                }
            }
        }
        if(getcustom('shop_other_infor')){
            if($datalist){
                $sysset = Db::name('admin_set')->where('aid',aid)->field('name,main_business,tel')->find();
                foreach($datalist as &$v){
                    //特别标识
                    $v['is_soi'] = 1;
                    
                    //联系系统名称
                    $v['lx_name'] = $sysset['name']?$sysset['name']:'';
                    //联系商家id
                    $v['lx_bid']  = $v['bid'];
                    //联系商家名称
                    $v['lx_bname']  = '';
                    //联系电话
                    $v['lx_tel']  = '';

                    $v['merchant_name'] = '';
                    $v['main_business'] = '';
                    //查询商家
                    if($v['bid']>0){
                        $merchant_name =  Db::name('business')
                            ->where('id',$v['bid'])
                            ->where('aid',aid)
                            ->field('name,main_business,tel')
                            ->find();
                        if($merchant_name){
                            $v['merchant_name'] = $merchant_name['name'];
                            $v['main_business'] = $merchant_name['main_business'];

                            //联系商家名称
                            $v['lx_bname']  = $merchant_name['name']?$merchant_name['name']:'';
                            //联系电话
                            $v['lx_tel']    = $merchant_name['tel']?$merchant_name['tel']:'';
                        }
                    }else{
                        $v['merchant_name'] = $sysset['name'];
                        $v['main_business'] = $sysset['main_business'];

                        //联系电话
                        $v['lx_tel']    = $sysset['tel']?$sysset['tel']:'';
                    }


                }
                unset($v);
            }
        }
		$datalist = $this->formatprolist($datalist);
		if(getcustom('price_dollar')){
			$usdrate = Db::name('shop_sysset')->where('aid',aid)->value('usdrate');
			foreach($datalist as &$d){
				if($usdrate>0){
					$d['showprice_dollar'] = true;
					$d['usd_sellprice'] = round($d['sell_price']/$usdrate,2);
				}
			}
		}

        //价格显示方式
        if(getcustom('price_show_type')){
            if($this->member){
                $userlevel = Db::name('member_level')->where('aid',aid)->where('id',$this->member['levelid'])->find();
            }
           
            $price_show_type = Db::name('shop_sysset')->where('aid',aid)->value('price_show_type');
            $defalut_level = Db::name('member_level')->where('aid',aid)->order('id asc')->find();
            foreach($datalist as $pk=>&$pv){
                $pv['price_show_type'] = $price_show_type;
               
                if(empty($userlevel) || $defalut_level['id'] == $this->member['levelid']){
                    $pv['is_vip'] =0;
                }else{
                    $pv['is_vip'] =1;
                }
                $pv['sell_price_origin'] = $pv['sell_price_origin'];
                if(in_array($price_show_type,[1,2])){ //开启会员价
                    $lvprice_data =json_decode($pv['lvprice_data'],true);
                    
                    if($pv['is_vip'] == 0){//不是会员 查询下个会员
                        $nextlevel = Db::name('member_level')->where('aid',aid)->where('sort','>',$userlevel['sort'])->order('sort,id')->find();
                        
                        if(empty($nextlevel) || $pv['lvprice'] ==0 ){
                            $nextlevel = Db::name('member_level')->where('aid',aid)->where('sort','>',$defalut_level['sort'])->order('sort,id')->find();
                        }
                        $level_name = $nextlevel['name'];
                        $pv['sell_price_origin'] = $lvprice_data[$nextlevel['id']];
                    }else{
                        if($userlevel && $pv['lvprice'] ==1 ){
                            $level_name = $userlevel['name'];
                        }
                    }
                    $pv['level_name_show'] =  $level_name;
                }
                
            }
        }
		
        //价格显示方式
        if(getcustom('price_show_type')){
            if($this->member){
                $userlevel = Db::name('member_level')->where('aid',aid)->where('id',$this->member['levelid'])->find();
            }
            foreach($datalist as &$pv){
                if($userlevel && $pv['lvprice'] ==1 ){
                    $pv['level_name'] =  $userlevel['name'];
                }
                if($pv['lvprice_data']){
                    $lvprice_data = json_decode($pv['lvprice_data'],true);
                    $pv['level_price'] = $lvprice_data[$this->member['levelid']];
                }else{
                    $pv['level_price'] = 0;
                }
                
            }
            
            $defalut_level = Db::name('member_level')->where('aid',aid)->order('id asc')->find();
            if($defalut_level['id'] == $this->member['levelid']){
                $product['is_vip'] =0;
            }else{
                $product['is_vip'] =1;
            }
        }
		//未登录查看价格
		if(getcustom('show_price_unlogin')){
			$shopset = Db::name('shop_sysset')->where('aid', aid)->find();
			$mid = mid;
			if(!$mid && $shopset['is_show_price_unlogin'] == 0){
				foreach($datalist as &$pv){
					$pv['sell_price'] =  $shopset['show_price_unlogin_txt'];					
				}
			}			
		}
		//未审核查看价格
		if(getcustom('show_price_uncheck')){
			$shopset = Db::name('shop_sysset')->where('aid', aid)->find();
			if(mid && $this->member['checkst'] !=1 && $shopset['is_show_price_uncheck'] == 0){
				foreach($datalist as &$pv){
					$pv['sell_price'] =  $shopset['show_price_uncheck_txt'];					
				}
			}			
		}
        
		return $this->json(['status'=>1,'data'=>$datalist]);
	}
	public function prolist(){
        $where2 = "find_in_set('-1',showtj)";
        if($this->member){
            $where2 .= " or find_in_set('".$this->member['levelid']."',showtj)";
            if($this->member['subscribe']==1){
                $where2 .= " or find_in_set('0',showtj)";
            }
        }
        $tjwhere[] = Db::raw($where2);
		//分类
		if(input('param.cid')){
			$clist = Db::name('shop_category')->where($tjwhere)->where('aid',aid)->where('pid',input('param.cid/d'))->where('status',1)->order('sort desc,id')->select()->toArray();
			if(!$clist) $clist = [];
		}else{
			$clist = Db::name('shop_category')->where($tjwhere)->where('aid',aid)->where('pid',0)->where('status',1)->order('sort desc,id')->select()->toArray();
			if(!$clist) $clist = [];
		}
		if(input('param.bid')){
			$bid = input('param.bid/d');
			if(input('param.cid2')){
				$clist2 = Db::name('shop_category2')->where('aid',aid)->where('bid',$bid)->where('pid',input('param.cid2/d'))->where('status',1)->order('sort desc,id')->select()->toArray();
				if(!$clist2) $clist2 = [];
			}else{
				$clist2 = Db::name('shop_category2')->where('aid',aid)->where('bid',$bid)->where('pid',0)->where('status',1)->order('sort desc,id')->select()->toArray();
				if(!$clist2) $clist2 = [];
			}
		}else{
			$clist2 = [];
		}
		//分组
		$glist = Db::name('shop_group')->where('aid',aid)->where('status',1)->select()->toArray();
		if(!$glist) $glist = [];
		
		$rsset=[];
        if(getcustom('image_search')){
            $image_search = Db::name('admin')->where('id',aid)->value('image_search');
            if($image_search == 1) {
                $set = Db::name('baidu_set')->where('aid',aid)->find();
                $rsset['image_search'] = $set['image_search'];
            }
        }
		$topshowcategory = 0;
        if(getcustom('ngmm')){
			$topshowcategory = 1;
			if(!$clist && input('param.cid')){
				$thiscinfo = Db::name('shop_category')->where('aid',aid)->where('id',input('param.cid'))->find();
				$clist = Db::name('shop_category')->where('aid',aid)->where('pid',$thiscinfo['pid'])->where('status',1)->order('sort desc,id')->select()->toArray();
			}
		}

		$rdata = [];
		$rdata['clist'] = $clist;
		$rdata['glist'] = $glist;
		$rdata['clist2'] = $clist2;
		$rdata['set'] = $rsset;
		$rdata['topshowcategory'] = $topshowcategory;

		if(getcustom('prolist_showjuli')){
			$rdata['needlocation'] = true;
		}

		return $this->json($rdata);
	}
	public function getparamlist(){
		//参数
        $whereParam = [];
        $whereParam[] = ['aid','=',aid];
        $whereParam[] = ['status','=',1];
        $whereParam[] = ['type','=',1];
		if(input('param.bid')){
			$bid = input('param.bid/d');
		}else{
			$bid = 0;
		}
        if(input('param.cid') && input('param.cid') != 'undefined'){
			$cid = input('param.cid');
            if($cid){
				if(getcustom('business_showplatparam') && $bid > 0){
					$whereParam[] = Db::raw("(bid=0 and find_in_set({$cid},cid)) or (bid={$bid} and find_in_set({$cid},cid) or cid =''))");
				}else{
					$whereParam[] = ['bid','=',$bid];
					$whereParam[] = Db::raw("find_in_set({$cid},cid) or cid =''");
				}
            }else{
				$whereParam[] = ['bid','=',$bid];
                $whereParam[] = Db::raw("cid =''");
			}
        }else{
			$whereParam[] = ['bid','=',$bid];
            $whereParam[] = Db::raw(" cid =''");
        }
		$paramList = Db::name('shop_param')->where($whereParam)->order('sort desc,id')->select()->toArray();
		if(!$paramList) $paramList = [];
		foreach($paramList as $k=>$v){
			$paramList[$k]['params'] = json_decode($v['params'],true);
		}
		$rdata = [];
		$rdata['status'] = 1;
		$rdata['data'] = $paramList;
		return $this->json($rdata);
	}
	
	//分类商品
	public function classify(){
		if(input('param.bid')){
			$clist = Db::name('shop_category2')->where('aid',aid)->where('bid',input('param.bid/d'))->where('pid',0)->where('status',1)->order('sort desc,id')->select()->toArray();
			foreach($clist as $k=>$v){
				$rs = Db::name('shop_category2')->where('aid',aid)->where('pid',$v['id'])->where('status',1)->order('sort desc,id')->select()->toArray();
				if(!$rs) $rs = [];
				$clist[$k]['child'] = $rs;
			}
		}else{
            $where2 = "find_in_set('-1',showtj)";
            if($this->member){
                $where2 .= " or find_in_set('".$this->member['levelid']."',showtj)";
                if($this->member['subscribe']==1){
                    $where2 .= " or find_in_set('0',showtj)";
                }
            }
            $tjwhere[] = Db::raw($where2);
			$clist = Db::name('shop_category')->where($tjwhere)->where('aid',aid)->where('pid',0)->where('status',1)->order('sort desc,id')->select()->toArray();
//			dd($clist);
			foreach($clist as $k=>$v){
				$rs = Db::name('shop_category')->where($tjwhere)->where('aid',aid)->where('pid',$v['id'])->where('status',1)->order('sort desc,id')->select()->toArray();
				if(!$rs) $rs = [];
				$clist[$k]['child'] = $rs;
			}
		}
		return $this->json(['status'=>1,'data'=>$clist]);
	}
	//获取子分类
	public function getdownclist(){
		$pid = input('param.id/d');
		$clist = Db::name('shop_category')->where('aid',aid)->where('pid',$pid)->where('status',1)->order('sort desc,id')->select()->toArray();
		if(!$clist) $clist = [];
		return $this->json(['status'=>1,'data'=>$clist]);
	}
	//分类商品样式二
	public function classify2(){
		if(input('param.bid')){
			$bid = input('param.bid/d');
			$clist = Db::name('shop_category2')->where('aid',aid)->where('bid',$bid)->where('pid',0)->where('status',1)->order('sort desc,id')->select()->toArray();
			foreach($clist as $k=>$v){
				$child = Db::name('shop_category2')->where('aid',aid)->where('bid',$bid)->where('pid',$v['id'])->where('status',1)->order('sort desc,id')->select()->toArray();
				if(!$child) $child = [];
				foreach($child as $k2=>$v2){
					$child2 = Db::name('shop_category2')->where('aid',aid)->where('bid',$bid)->where('pid',$v2['id'])->where('status',1)->order('sort desc,id')->select()->toArray();
					$child[$k2]['child'] = $child2;
				}
				$clist[$k]['child'] = $child;
			}
		}else{
            $where2 = "find_in_set('-1',showtj)";
            if($this->member){
                $where2 .= " or find_in_set('".$this->member['levelid']."',showtj)";
                if($this->member['subscribe']==1){
                    $where2 .= " or find_in_set('0',showtj)";
                }
            }
            $tjwhere[] = Db::raw($where2);
			$clist = Db::name('shop_category')->where($tjwhere)->where('aid',aid)->where('pid',0)->where('status',1)->order('sort desc,id')->select()->toArray();
			foreach($clist as $k=>$v){
				$child = Db::name('shop_category')->where($tjwhere)->where('aid',aid)->where('pid',$v['id'])->where('status',1)->order('sort desc,id')->select()->toArray();
				if(!$child) $child = [];
				foreach($child as $k2=>$v2){
					$child2 = Db::name('shop_category')->where($tjwhere)->where('aid',aid)->where('pid',$v2['id'])->where('status',1)->order('sort desc,id')->select()->toArray();
					$child[$k2]['child'] = $child2;
				}
				$clist[$k]['child'] = $child;
			}
		}
		return $this->json(['status'=>1,'data'=>$clist]);
	}

	//一级分类
	public function category1(){
		if(input('param.bid')){
			$list = Db::name('shop_category2')->where('aid',aid)->where('bid',input('param.bid/d'))->where('pid',0)->where('status',1)->order('sort desc,id')->select()->toArray();
		}else{
            $where2 = "find_in_set('-1',showtj)";
            if($this->member){
                $where2 .= " or find_in_set('".$this->member['levelid']."',showtj)";
                if($this->member['subscribe']==1){
                    $where2 .= " or find_in_set('0',showtj)";
                }
            }
            $tjwhere[] = Db::raw($where2);
			$list = Db::name('shop_category')->where($tjwhere)->where('aid',aid)->where('pid',0)->where('status',1)->order('sort desc,id')->select()->toArray();
		}
		return $this->json(['status'=>1,'data'=>$list]);
	}
	//二级分类
	public function category2(){
		if(input('param.bid')){
			$cid = input('param.cid/d');
			$bid = input('param.bid/d');
			if($cid){
				$rs = Db::name('shop_category2')->where('aid',aid)->where('bid',$bid)->where('pid',$cid)->where('status',1)->order('sort desc,id')->select()->toArray();
				if(!$rs) $rs = array();
				$list = $rs;
			}else{
				$list = Db::name('shop_category2')->where('aid',aid)->where('bid',$bid)->where('pid',0)->where('status',1)->order('sort desc,id')->select()->toArray();
				if(!$list) $list = array();
				foreach($list as $k=>$v){
					$rs = Db::name('shop_category2')->where('aid',aid)->where('bid',$bid)->where('pid',$v['id'])->where('status',1)->order('sort desc,id')->select()->toArray();
					if(!$rs) $rs = array();
					$list[$k]['child'] = $rs;
				}
			}
		}else{
			$cid = input('param.cid/d');
            $where2 = "find_in_set('-1',showtj)";
            if($this->member){
                $where2 .= " or find_in_set('".$this->member['levelid']."',showtj)";
                if($this->member['subscribe']==1){
                    $where2 .= " or find_in_set('0',showtj)";
                }
            }
            $tjwhere[] = Db::raw($where2);
			if($cid){
				$rs = Db::name('shop_category')->where($tjwhere)->where('aid',aid)->where('pid',$cid)->where('status',1)->order('sort desc,id')->select()->toArray();
				if(!$rs) $rs = array();
				$list = $rs;
			}else{
				$list = Db::name('shop_category')->where($tjwhere)->where('aid',aid)->where('pid',0)->where('status',1)->order('sort desc,id')->select()->toArray();
				if(!$list) $list = array();
				foreach($list as $k=>$v){
					$rs = Db::name('shop_category')->where($tjwhere)->where('aid',aid)->where('pid',$v['id'])->where('status',1)->order('sort desc,id')->select()->toArray();
					if(!$rs) $rs = array();
					$list[$k]['child'] = $rs;
				}
			}
		}
		return $this->json(['status'=>1,'data'=>$list]);
	}
	//三级分类
	public function category3(){
		if(input('param.bid')){
			$bid = input('param.bid/d');
			$list = Db::name('shop_category2')->where('aid',aid)->where('bid',$bid)->where('pid',0)->where('status',1)->order('sort desc,id')->select()->toArray();
		}else{
            $where2 = "find_in_set('-1',showtj)";
            if($this->member){
                $where2 .= " or find_in_set('".$this->member['levelid']."',showtj)";
                if($this->member['subscribe']==1){
                    $where2 .= " or find_in_set('0',showtj)";
                }
            }
            $tjwhere[] = Db::raw($where2);
			$list = Db::name('shop_category')->where($tjwhere)->where('aid',aid)->where('pid',0)->where('status',1)->order('sort desc,id')->select()->toArray();
		}
		$rdata = [];
		$rdata['data'] = $list;
		return $this->json($rdata);
	}
	//获取二三分类
	public function getdownclist3(){
		$pid = input('param.id/d');
		if(input('param.bid')){
			$bid = input('param.bid/d');
			$clist = Db::name('shop_category2')->where('aid',aid)->where('bid',$bid)->where('pid',$pid)->where('status',1)->order('sort desc,id')->select()->toArray();
			if(!$clist) $clist = [];
			foreach($clist as $k=>$v){
				$rs = Db::name('shop_category2')->where('aid',aid)->where('bid',$bid)->where('pid',$v['id'])->where('status',1)->order('sort desc,id')->select()->toArray();
				if(!$rs) $rs = array();
				$clist[$k]['child'] = $rs;
			}
		}else{
			$clist = Db::name('shop_category')->where('aid',aid)->where('pid',$pid)->where('status',1)->order('sort desc,id')->select()->toArray();
			if(!$clist) $clist = [];
			foreach($clist as $k=>$v){
				$rs = Db::name('shop_category')->where('aid',aid)->where('pid',$v['id'])->where('status',1)->order('sort desc,id')->select()->toArray();
				if(!$rs) $rs = array();
				$clist[$k]['child'] = $rs;
			}
		}
		return $this->json(['status'=>1,'data'=>$clist]);
	}
	
	//商品
	public function product(){
		$proid = input('param.id/d');
        //判断是否需要经纬度【如果门店模式，显示最近门店，经纬度必须】
        $needlocation = false;
        if(getcustom('product_bind_mendian')){
            $latitude = input('param.latitude','');
            $longitude = input('param.longitude','');
            $mendian_id = input('param.mendian_id/d','');
            if((empty($latitude) || empty($longitude))){
                $needlocation = true;
            }
        }

		$product = Db::name('shop_product')->where('id',$proid)->where('aid',aid)->find();
       
		if(!$product) return $this->json(['status'=>0,'msg'=>'商品不存在']);
		if($product['status']==0) return $this->json(['status'=>0,'msg'=>'商品未上架']);
		if($product['ischecked']!=1) return $this->json(['status'=>0,'msg'=>'商品未审核']);
		
		if($product['status']==2 && (strtotime($product['start_time']) > time() || strtotime($product['end_time']) < time())){
			return $this->json(['status'=>0,'msg'=>'商品未上架']);
		}
		if($product['status']==3){
			$start_time = strtotime(date('Y-m-d '.$product['start_hours']));
			$end_time = strtotime(date('Y-m-d '.$product['end_hours']));
			if(($start_time < $end_time && ($start_time > time() || $end_time < time())) || ($start_time >= $end_time && ($start_time > time() && $end_time < time()))){
				return $this->json(['status'=>0,'msg'=>'商品未上架']);
			}
		}

		$product['paramdata'] = json_decode($product['paramdata'],true);
		if(!$product['paramdata']) $product['paramdata'] = [];
		foreach($product['paramdata'] as $k=>$v){
			if(is_array($v)){
				$product['paramdata'][$k] = implode(' ',$v);
			}
			if(!$v) unset($product['paramdata'][$k]);
		}

		//显示条件
        $levelids = explode(',',$product['showtj']);
        //限制等级
        if(!in_array('-1',$levelids)){
            $showtj1 = false;
            $showtj2 = false;
            //-1 未登录
            if(!in_array('-2',$levelids) && !$this->member){
                $this->checklogin();
            }
            if(in_array('-2',$levelids) && !$this->member){
                $showtj1 = true;
            }
            if(in_array($this->member['levelid'], $levelids)) {
                $showtj1 = true;
            }
            if(in_array('0',$levelids) && $this->member['subscribe']==1){
                $showtj2 = true;
            }
            if(!$showtj1 && !$showtj2){
                return $this->json(['status'=>0,'msg'=>'商品状态不可见']);
            }
        }
       
        //商品分类显示条件
        if(getcustom('product_cat_showtj')) {
            $cidArr = explode(',',$product['cid']);
            if($cidArr) {
                $clist = Db::name('shop_category')->where('id','in',$cidArr)->where('aid',aid)->select()->toArray();
                foreach($clist as $k => $crow){
                    if($crow['showtj'] > 0) {
                        $this->checklogin();
                        //限制等级
                        $levelids = explode(',',$crow['showtj']);
                        if(!in_array($this->member['levelid'], $levelids)) {
                            return $this->json(['status'=>0,'msg'=>'商品分类状态不可见']);
                        }
                    } elseif($crow['showtj'] == 0) {
                        $this->checklogin();
                        //关注用户
                        if($this->member['subscribe']!=1){
                            return $this->json(['status'=>0,'msg'=>'商品分类状态不可见']);
                        }
                    }
                }
            }

            $where2 = "find_in_set('-1',showtj)";
            if($this->member){
                $where2 .= " or find_in_set('".$this->member['levelid']."',showtj)";
                if($this->member['subscribe']==1){
                    $where2 .= " or find_in_set('0',showtj)";
                }
            }
            $tjwhere[] = Db::raw($where2);
            $clist = Db::name('shop_category')->where($tjwhere)->where('aid',aid)->column('id');
            if($clist){
                foreach($clist as $k => $c2){
                    $whereCid[] = "find_in_set({$c2},cid)";
                }
                $where[] = Db::raw(implode(' or ',$whereCid));
            }
        }
        if(getcustom('pay_yuanbao')) {
            $yuanbao_money_ratio = 0;
            $yb_set = Db::name('admin_set')->where('aid',aid)->field('yuanbao_money_ratio')->find();
            if($yb_set){
                $yuanbao_money_ratio = $yb_set['yuanbao_money_ratio'];
            }
            $need_money = $product['yuanbao']*$yuanbao_money_ratio/100;
            $need_money = round($need_money,2);
            $product['yuanbao'] = $product['yuanbao'].t('元宝');
        }else{
            $product['yuanbao'] = '';
        }

        if(getcustom('plug_tengrui')) {
            //判断是否是否符合会员认证、会员关系、一户、用户组
            $tr_check = new \app\common\TengRuiCheck();
            $check_product = $tr_check->check_product($this->member,$product,1);
            if($check_product && $check_product['status'] == 0 ){
                return $this->json(['status'=>$check_product['status'],'msg'=>$check_product['msg']]);
            }
            $tr_roomId = $check_product['tr_roomId'];
        }
        $showMoneyPrice = false;
        if(getcustom('product_show_moneyprice')){
            if($product['product_moneypay']==1 || $product['product_moneypay']==2){
                $showMoneyPrice = true;
            }
        }


		$product['isgm'] = true;
		if(getcustom('shopcate_time')){
			$procids = explode(',',$product['cid']);
			$cate = Db::name('shop_category')->where('aid',aid)->where('id','in',$product['cid'])->select()->toArray();
			foreach($cate as $c){
				if($c['start_hours'] && $c['end_hours']){
					$catestart_time =  strtotime(date('Y-m-d '.$c['start_hours']));
					$cateend_time =  strtotime(date('Y-m-d '.$c['end_hours']));
					if(($catestart_time < $cateend_time && ($catestart_time > time() || $cateend_time < time())) || ($catestart_time >= $cateend_time && ($catestart_time > time() && $cateend_time < time()))){
						$product['isgm']=false;
					}
				}
				
			}
		}

		if($product['status']==2 || $product['status']==3) $product['status']=1;

		if(!$product['pics']) $product['pics'] = $product['pic'];
		$product['pics'] = explode(',',$product['pics']);
		$oldproduct = $product;
		$product = $this->formatproduct($product);

		if($product['video']){
			$minute = floor($product['video_duration']/60);
			if($minute < 10) $minute = '0'.$minute;
			$second = $product['video_duration']%60;
			if($second < 10) $second = '0'.$second;
			$product['video_duration'] = $minute . "'" . $second . '"';
		}else{
            $product['video'] = '';
        }

		//是否收藏
		$rs = Db::name('member_favorite')->where('aid',aid)->where('mid',mid)->where('proid',$proid)->where('type','shop')->find();
		if($rs){
			$isfavorite = true;
		}else{
			$isfavorite = false;
		}
		//获取评论
		$commentlist = Db::name('shop_comment')->where('aid',aid)->where('proid',$proid)->where('status',1)->order('id desc')->limit(10)->select()->toArray();
		if(!$commentlist) $commentlist = [];
		foreach($commentlist as $k=>$pl){
			$commentlist[$k]['createtime'] = date('Y-m-d H:i',$pl['createtime']);
			if($commentlist[$k]['content_pic']) $commentlist[$k]['content_pic'] = explode(',',$commentlist[$k]['content_pic']);
		}
		$commentcount = Db::name('shop_comment')->where('aid',aid)->where('proid',$proid)->where('status',1)->count();
		//添加浏览历史
		if(mid){
			$rs = Db::name('member_history')->where('aid',aid)->where('mid',mid)->where('proid',$proid)->where('type','shop')->find();
			if($rs){
				Db::name('member_history')->where('id',$rs['id'])->update(['createtime'=>time()]);
			}else{
				Db::name('member_history')->insert(['aid'=>aid,'mid'=>mid,'proid'=>$proid,'type'=>'shop','createtime'=>time()]);
			}
		}
        $custom = ['isshowall'=>true];
		$sysset = Db::name('admin_set')->where('aid',aid)->field('name,logo,desc,fxjiesuantype,tel,kfurl,gzts,ddbb')->find();

		$shopset_field = 'showjd,comment,showcommission,hide_sales,hide_stock,show_lvupsavemoney';
		if(getcustom('shop_prodetailtitle')){
			$shopset_field.=',prodetailtitle_type,prodetailtitle_value';
		}
        if(getcustom('product_comment')){
            $shopset_field.=',product_comment';
        }
        $custom_show_price = false;
        if(getcustom('product_cost_show')){
            $custom_show_price = true;
            $shopset_field.=',hide_cost,cost_name,cost_color';
        }
        if(getcustom('product_sellprice_show')){
            $custom_show_price = true;
            $shopset_field.=',hide_sellprice,sellprice_name,sellprice_color';
        }

        if(getcustom('product_detail_special')){
            $custom['product_detail_special'] = true;
            $custom_show_price = true;
            $shopset_field.=',show_product_name,show_guige,show_option_group,show_header_pic';
        }
		if(getcustom('product_guige_showtype')){
            $custom['product_guige_showtype'] = true;
            $shopset_field.=',guige_name,show_guigetype';
        }
		if(getcustom('shop_gwc_name')){
            $custom['shop_gwc_name'] = true;
            $shopset_field.=',gwc_showst,gwc_name';
        }
		if(getcustom('product_commission_desc')){
            $custom['product_commission_desc'] = true;
            $shopset_field.=',commission_desc';
        }
		//未登录查看价格
		if(getcustom('show_price_unlogin')){
			$shopset_field.=',is_show_price_unlogin,show_price_unlogin_txt';		
		}
		//未审核查看价格
		if(getcustom('show_price_uncheck')){
			$shopset_field.=',is_show_price_uncheck,show_price_uncheck_txt';		
		}
		$shopset = Db::name('shop_sysset')->where('aid',aid)->field($shopset_field)->find();
        $custom['show_price'] = $custom_show_price;
        if($this->member){
            $userlevel = Db::name('member_level')->where('aid',aid)->where('id',$this->member['levelid'])->find();
        }

		
		//预计佣金
		$commission = 0;
        $product['commission_desc'] = '元';
        $product['commission_desc_score'] = t('积分');
		if($this->member && $shopset['showcommission']==1){
			if($userlevel['can_agent']!=0){
				if($product['commissionset']==1){//按比例
					$commissiondata = json_decode($product['commissiondata1'],true);
					if($commissiondata){
						$commission = $commissiondata[$userlevel['id']]['commission1'] * ($product['sell_price'] - ($sysset['fxjiesuantype']==2 ? $product['cost_price'] : 0)) * 0.01;
					}
				}elseif($product['commissionset']==2){//按固定金额
					$commissiondata = json_decode($product['commissiondata2'],true);
					if($commissiondata){
						$commission = $commissiondata[$userlevel['id']]['commission1'];
					}
                }elseif($product['commissionset']==3) {//提成是积分
                    $commissiondata = json_decode($product['commissiondata3'],true);
                    if($commissiondata){
                        $commissionScore = $commissiondata[$userlevel['id']]['commission1'];
                    }
                //elseif($product['commissionset']==4 && $product['lvprice']==1)//按价格差
				//	$lvprice_data = json_decode($product['lvprice_data'],true);
                //    $commission = array_shift($lvprice_data) - $product['sell_price_origin'];
				//	if($commission < 0) $commission = 0;
                }elseif($product['commissionset']==5){//提成比例+积分
                    $commissiondata = json_decode($product['commissiondata1'],true);
                    if($commissiondata){
                        $commission = $commissiondata[$userlevel['id']]['commission1'] * ($product['sell_price'] - ($sysset['fxjiesuantype']==2 ? $product['cost_price'] : 0)) * 0.01;
                    }
                    $commissiondataScore = json_decode($product['commissiondata3'],true);
                    if($commissiondataScore){
                        $commissionScore = $commissiondataScore[$userlevel['id']]['commission1'];
                    }
                }elseif($product['commissionset']==6){//提成金额+积分
                    $commissiondata = json_decode($product['commissiondata2'],true);
                    if($commissiondata){
                        if(getcustom('fengdanjiangli') && $product['fengdanjiangli']){

                        }else{
                            $commission = $commissiondata[$userlevel['id']]['commission1'] *1;
                        }
                    }
                    $commissiondataScore = json_decode($product['commissiondata3'],true);
                    if($commissiondataScore){
                        $commissionScore = $commissiondataScore[$userlevel['id']]['commission1'];
                    }
                }elseif($product['commissionset']==7){//分销按比例送积分
                    $commissiondata = json_decode($product['commissiondata4'],true);
                    if($commissiondata){
                        $commissionScore = $commissiondata[$userlevel['id']]['commission1'] * ($product['sell_price'] - ($sysset['fxjiesuantype']==2 ? $product['cost_price'] : 0)) * 0.01;
                    }
                }elseif($product['commissionset']==0){//按会员等级
				    //fxjiesuantype 0按商品价格,1按成交价格,2按销售利润
					if($userlevel['commissiontype']==1){ //固定金额按单
						$commission = $userlevel['commission1'];
					}else{
						$commission = $userlevel['commission1'] * ($product['sell_price_origin'] - ($sysset['fxjiesuantype']==2 ? $product['cost_price'] : 0)) * 0.01;
					}
				}
				if($product['commissionset4']==1 && $product['lvprice']==1){ //极差分销
					$lvprice_data = json_decode($product['lvprice_data'],true);
					$commission += array_shift($lvprice_data) - $product['sell_price'];
					if($commission < 0) $commission = 0;
				}
			}
		}
		$product['commission'] = round($commission*100)/100;

        $score_weishu = $this->score_weishu;
        $product['givescore'] = dd_money_format($product['givescore'],$score_weishu);
        $product['commissionScore'] = dd_money_format($commissionScore,$score_weishu);
        if(isset($shopset['hide_cost']) && $shopset['hide_cost']==0){
            //显示成本
        }else{
            unset($product['cost_price']);
        }
		//节省金额
		$product['jiesheng_money'] = round($oldproduct['sell_price'] - $product['sell_price'],2);
		if($product['jiesheng_money'] <= 0) $product['jiesheng_money'] = round($product['market_price'] - $product['sell_price'],2);
		if($product['jiesheng_money'] <= 0) $product['jiesheng_money'] = 0;

		//升级到下一等级预计节省多少钱
		if($shopset['show_lvupsavemoney'] == 1 && $this->member){
			$upsavemoney = 0;
			$nextlevel = Db::name('member_level')->where('aid',aid)->where('sort','>',$userlevel['sort'])->order('sort,id')->find();
			if($nextlevel){
				$sell_price = $oldproduct['sell_price'];
				if($product['lvprice']==0 && $product['no_discount'] == 0 && $userlevel['discount'] > 0 && $userlevel['discount'] < 10){
					$this_sell_price = $sell_price * $userlevel['discount'] * 0.1;
				}else{
					if($product['lvprice']==1){
						$lvprice_data = json_decode($oldproduct['lvprice_data'],true);
						if($lvprice_data && isset($lvprice_data[$this->member['levelid']])){
							$this_sell_price = $lvprice_data[$this->member['levelid']];
						}
					}else{
						$this_sell_price = $sell_price;
					}
				}
				if($product['lvprice']==0 && $product['no_discount'] == 0 && $nextlevel['discount'] > 0 && $nextlevel['discount'] < 10){
					$next_sell_price = $sell_price * $nextlevel['discount'] * 0.1;
				}else{
					if($product['lvprice']==1){
						$lvprice_data = json_decode($oldproduct['lvprice_data'],true);
						if($lvprice_data && isset($lvprice_data[$nextlevel['id']])){
							$next_sell_price = $lvprice_data[$nextlevel['id']];
						}
					}else{
						$next_sell_price = $sell_price;
					}
				}
				if($this_sell_price > $next_sell_price){
					$upsavemoney = round($this_sell_price - $next_sell_price,2);
				}
			}
			$product['upsavemoney'] = $upsavemoney;
			$product['nextlevelname'] = $nextlevel ? $nextlevel['name'] : '';
		}else{
			$product['upsavemoney'] = 0;
		}

		if($product['balance'] > 0){
			$product['advance_price'] = round($product['sell_price'] * (1 - $product['balance'] *0.01),2);
			$product['balance_price'] = round($product['sell_price'] * $product['balance'] *0.01,2);
		}

		if(getcustom('buy_selectmember') && $this->member){
			if($userlevel['can_buyselect'] ==1){
				$product['buyselect_commission'] = round($userlevel['buyselect_commission'] * $product['sell_price'] * 0.01,2);
			}
		}

		if($product['bid']!=0){
            $bfield = 'id,name,logo,desc,tel,address,sales,kfurl';
            if(getcustom('buybutton_custom')){
                $bfield .= ',buybtn_status';
            }
            $business = Db::name('business')->where('aid',aid)->where('id',$product['bid'])->field($bfield)->find();
		}else{
			$business = $sysset;
		}
		$product['detail'] = \app\common\System::initpagecontent($product['detail'],aid,mid,platform);
		$product['comment_starnum'] = floor($product['comment_score']);

		if($this->member && $product['lvprice']==0 && $product['no_discount'] == 0 && $userlevel['discount'] > 0 && $userlevel['discount'] < 10){
			$product['discount_tips'] = ' '.$userlevel['name'].' 享'.floatval($userlevel['discount']).'折优惠';
		}else{
			$product['discount_tips'] = '';
		}
        
		$sysset['showgzts'] = false;
		//关注提示
		if(platform == 'mp'){
			$sysset['gzts'] = explode(',',$sysset['gzts']);
			if(in_array('2',$sysset['gzts']) && $this->member['subscribe']==0){
				$appinfo = \app\common\System::appinfo(aid,'mp');
				$sysset['qrcode'] = $appinfo['qrcode'];
				$sysset['gzhname'] = $appinfo['nickname'];
				$sysset['showgzts'] = true;
			}
		}
		//订单播报
		$bboglist = [];
		$sysset['ddbb'] = explode(',',$sysset['ddbb']);
		if(in_array('2',$sysset['ddbb'])){
			$bboglist = Db::name('shop_order_goods')
				->field('mid,name,createtime,proid')
				->where('aid',aid)
				->where('proid',$product['id'])
				->where('status','in','0,1,2,3')
				->where('createtime','>',time()-86400*10)
				->order('createtime desc')->limit(10)->select()->toArray();
			if(!$bboglist) $bboglist = [];
			foreach($bboglist as $k=>$og){
				$ogmember = Db::name('member')->where('id',$og['mid'])->find();
				if(!$ogmember){
					unset($bboglist[$k]);
					continue;
				}else{
					$bboglist[$k]['nickname'] = $ogmember['nickname'];
					$bboglist[$k]['headimg'] = $ogmember['headimg'];
				}
				if(time() - $og['createtime'] < 60*5){
					$bboglist[$k]['showtime'] = '刚刚';
				}elseif(date('Ymd')==date('Ymd',$og['createtime'])){
					if($og['createtime'] + 3600 > time()){
						$bboglist[$k]['showtime'] = floor((time()-$og['createtime'])/60).'分钟前';
					}else{
						$bboglist[$k]['showtime'] = floor((time()-$og['createtime'])/3600).'小时前';
					}
				}elseif(time()-$og['createtime']<86400){
					$bboglist[$k]['showtime'] = '昨天';
				}elseif(time()-$og['createtime']<2*86400){
					$bboglist[$k]['showtime'] = '前天';
				}else{
					$bboglist[$k]['showtime'] = '三天前';
				}
			}
		}
		//促销活动
		$cuxiaolist = Db::name('cuxiao')
			->where('aid',aid)
			->where('bid',$product['bid'])
			->where('starttime','<',time())
			->where('endtime','>',time())
			->order('sort desc')->select()->toArray();
		$newcxlist = [];
		foreach($cuxiaolist as $k=>$v){
			$gettj = explode(',',$v['gettj']);
			if(!in_array('-1',$gettj) && !in_array($this->member['levelid'],$gettj)){ //不是所有人
				continue;
			}
			if($v['fwtype']==2){//指定商品可用
				$productids = explode(',',$v['productids']);
				if(!in_array($product['id'],$productids)){
					continue;
				}
			}
			if($v['fwtype']==1){//指定类目可用
				if(!$v['categoryids']) continue;
				$categoryids = explode(',',$v['categoryids']);
				$cids = explode(',',$product['cid']);
				$clist = Db::name('shop_category')->where('pid','in',$categoryids)->select()->toArray();
				foreach($clist as $kc=>$vc){
					$categoryids[] = $vc['id'];
					$cate2 = Db::name('shop_category')->where('pid',$vc['id'])->find();
					if($cate2) $categoryids[] = $cate2['id'];
				}
				if(!array_intersect($cids,$categoryids)){
					continue;
				}
			}
			$newcxlist[] = $v;
		}
		//优惠券isgive=2 仅转赠的卡券，不可自己使用
		$couponlist = Db::name('coupon')->where('aid',aid)->where('bid',$product['bid'])->where('isgive','<>',2)->where('tolist',1)->where('type','in','1,4,10')->where("unix_timestamp(starttime)<=".time()." and unix_timestamp(endtime)>=".time())->order('sort desc,id desc')->select()->toArray();
		$newcplist = [];
		foreach($couponlist as $k=>$v){
			$showtj = explode(',',$v['showtj']);
			if(!in_array('-1',$showtj) && !in_array($this->member['levelid'],$showtj)){ //不是所有人
				continue;
			}
            //0全场通用,1指定类目,2指定商品
            if(!in_array($v['fwtype'],[0,1,2])){
                continue;
            }
			if($v['fwtype']==2){//指定商品可用
				$productids = explode(',',$v['productids']);
				if(!in_array($product['id'],$productids)){
					continue;
				}
			}
			if($v['fwtype']==1){//指定类目可用
				if(!$v['categoryids']) continue;
				$categoryids = explode(',',$v['categoryids']);
				$cids = explode(',',$product['cid']);
				$clist = Db::name('shop_category')->where('pid','in',$categoryids)->select()->toArray();
				foreach($clist as $kc=>$vc){
					$categoryids[] = $vc['id'];
					$cate2 = Db::name('shop_category')->where('pid',$vc['id'])->find();
					if($cate2) $categoryids[] = $cate2['id'];
				}
				if(!array_intersect($cids,$categoryids)){
					continue;
				}
			}
			$haveget = Db::name('coupon_record')->where('aid',aid)->where('mid',mid)->where('couponid',$v['id'])->count();
			$v['haveget'] = $haveget;
			//$v['starttime'] = date('m-d H:i',strtotime($v['starttime']));
			//$v['endtime'] = date('m-d H:i',strtotime($v['endtime']));
            if($v['yxqtype'] == 1){
                $yxqtime = explode(' ~ ',$v['yxqtime']);
                $v['yxqdate'] = strtotime($yxqtime[1]);
            }elseif($v['yxqtype'] == 2){
                $v['yxqdate'] = time() + 86400 * $v['yxqdate'];
            }elseif($v['yxqtype'] == 3) {
                //次日起计算有效期
                $v['yxqdate'] = strtotime(date('Y-m-d')) + 86400 * ($v['yxqdate'] + 1) - 1;
            }
            if($v['bid'] > 0){
                $v['bname'] = Db::name('business')->where('aid',aid)->where('id',$v['bid'])->value('name');
            }
			$newcplist[] = $v;
		}

		//商品服务
		if($product['fwid']){
			$fuwulist = Db::name('shop_fuwu')->where('aid',aid)->where('status',1)->where('id','in',$product['fwid'])->order('sort desc,id')->select()->toArray();
		}else{
			$fuwulist = [];
		}
	
		$minprice = 999999999999999;
		$maxprice = 0;
		$gglist = Db::name('shop_guige')->where('proid',$product['id'])->select()->toArray();
        $gglist = $this->formatgglist($gglist,$product['bid'], $product['lvprice']);//if($product['lvprice']==1)
		foreach($gglist as $k=>$v){
			if($v['sell_price'] < $minprice){
				$minprice = $v['sell_price'];
			}
			if($v['sell_price'] > $maxprice){
				$maxprice = $v['sell_price'];
			}
		}
		
        
        $price_dollar = false;
		if(getcustom('price_dollar')){
			$shopsets = Db::name('shop_sysset')->where('aid',aid)->field('usdrate')->find();
			if($shopsets['usdrate']>0){
				$product['usdmin_price'] = round($minprice/$shopsets['usdrate'],2);
				$product['usdmax_price'] = round($maxprice/$shopsets['usdrate'],2);
                $price_dollar = true;
			}
		}


		$product['min_price'] = round($minprice,2);
		$product['max_price'] = round($maxprice,2);
		
        //价格显示方式
        if(getcustom('price_show_type')){
            $price_show_type = Db::name('shop_sysset')->where('aid',aid)->value('price_show_type');
            $sysset['price_show_type'] = $price_show_type;

            $defalut_level = Db::name('member_level')->where('aid',aid)->order('id asc')->find();
            if(empty($userlevel) || $defalut_level['id'] == $this->member['levelid']){
                $product['is_vip'] =0;
            }else{
                $product['is_vip'] =1;
            }
           
            if(in_array($price_show_type,[1,2])){ //开启会员价
                $lvprice_data =json_decode($product['lvprice_data'],true);
                if($product['is_vip'] == 0){//不是会员 查询下个会员
                    $nextlevel = Db::name('member_level')->where('aid',aid)->where('sort','>',$userlevel['sort'])->order('sort,id')->find();
                   
                    if(empty($nextlevel) || $product['lvprice'] ==0 ){
                        $nextlevel = Db::name('member_level')->where('aid',aid)->where('sort','>',$defalut_level['sort'])->order('sort,id')->find();
                    }
                    $level_name = $nextlevel['name'];
                    $product['vip_price'] = $lvprice_data[$nextlevel['id']];
                    $product['min_price'] = $product['sell_price'];
                    $product['max_price'] = $product['sell_price'];
                }else{
                   
                    if($userlevel && $product['lvprice'] ==1 ){
                        $level_name =  $userlevel['name'];
                    }
                    $product['vip_price'] = $product['sell_price'];
                    $product['min_price'] = $product['sell_price_origin'];
                    $product['max_price'] = $product['sell_price_origin'];
                }
                $product['level_name'] =  $level_name;
            }
        }

        if(getcustom('shoptongji3')){
			Db::name('shop_product')->where('id',$product['id'])->inc('viewnum')->update();
		}
		
		$tjdatalist = [];
		if($product['show_recommend'] == 1){
			$tjwhere = [];
			$tjwhere[] = ['aid','=',aid];
			$tjwhere[] = ['status','=',1];
			$tjwhere[] = ['ischecked','=',1];
			$where2 = "find_in_set('-1',showtj)";
			if($this->member){
				$where2 .= " or find_in_set('".$this->member['levelid']."',showtj)";
				if($this->member['subscribe']==1){
					$where2 .= " or find_in_set('0',showtj)";
				}
			}
			$tjwhere[] = Db::raw($where2);

			if($product['bid']){
				$tjwhere[] = ['bid','=',$product['bid']];
			}else{
				$business_sysset = Db::name('business_sysset')->where('aid',aid)->find();
				if(!$business_sysset || $business_sysset['status']==0 || $business_sysset['product_isshow']==0){
					$tjwhere[] = ['bid','=',0];
				}
			}
			$tjdatalist = Db::name('shop_product')->field("id,pic,name,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint,price_type")->where($tjwhere)->limit(8)->order(Db::raw('rand()'))->select()->toArray();
			if(!$tjdatalist) $tjdatalist = array();
			$tjdatalist = $this->formatprolist($tjdatalist);
		}elseif($product['show_recommend'] == 2){
			$tjdatalist = Db::name('shop_product')->where('aid',aid)->where('id','in',$product['recommend_productids'])->order(Db::raw('field(id,'.$product['recommend_productids'].')'))->select()->toArray();
		}
		
		$xcx_scheme = false;
		if(getcustom('xcx_scheme')){
			$xcx_scheme = true;
		}
        $shop_yuding = false;
        if(getcustom('shop_yuding')){
            $shop_yuding = true;
        }
        if(getcustom('buybutton_custom')){
            $buybtn_name      = '';
            $buybtn_link_url = '';
            $admin = Db::name('admin')->where('id',aid)->field('id,buybtn_status')->find();
            if($admin && $admin['buybtn_status'] == 1){
                if($product['bid']!=0){
                    if($business && $business['buybtn_status'] == 1){
                        $buybtn_name     = $product['buybtn_name'];
                        $buybtn_link_url = $product['buybtn_link_url'];
                    }
                }else{
                    $buybtn_name     = $product['buybtn_name'];
                    $buybtn_link_url = $product['buybtn_link_url'];
                }
            }
            $product['buybtn_name']     = $buybtn_name;
            $product['buybtn_link_url'] = $buybtn_link_url;
        }

        //是否可自提
        $bindmendianlist = [];
        if(getcustom('product_bind_mendian')){
            $freightlist = Db::name('freight')->where('aid',aid)->where('bid',$product['bid'])->where('status',1)->where('pstype',1)->column('id','id');
            if($freightlist){
                //该商品是否可自提
                if($product['freighttype']==1){
                    $product['can_ziti'] = 1;
                }else if($product['freighttype']==0){
                    $freightdata = $product['freightdata']?explode(',',$product['freightdata']):[];
                    foreach ($freightdata as $freightid){
                        if(isset($freightlist[$freightid])){
                            $product['can_ziti'] = 1;
                            break;
                        }
                    }
                }
                if($product['can_ziti']==1){
                    //如果是全部门店，则取所有门店中最近的一个，如果是部分门店,则取售卖门店中最近的一个
                    $mdwhere = [];
                    $mdwhere[] = ['aid','=',aid];
					if(getcustom('business_platform_auth')){
						if($product['bid']===0){
							$business = Db::name('business')->where('aid',aid)->where('isplatform_auth',1)->where('status',1)->column('id');		
							array_push($business,$product['bid']);
							$mdwhere[] = ['bid','in',$business];
						}else{
							$mdwhere[] = ['bid','=',$product['bid']];
						}
					}else{
						$mdwhere[] = ['bid','=',$product['bid']];
					}
                    $mdwhere[] = ['status','=',1];
                    $bindMendianIds = $product['bind_mendian_ids']?explode(',',$product['bind_mendian_ids']):[];
                    if($bindMendianIds && !in_array('-1',$bindMendianIds)){
                        $mdwhere[] = ['id','in',explode(',',$product['bind_mendian_ids'])];
                    }
                    $mdfield = '*';
                    if($longitude && $latitude){
                       $mdfield .= ",round(6378.138*2*asin(sqrt(pow(sin( ({$latitude}*pi()/180-latitude*pi()/180)/2),2)+cos({$latitude}*pi()/180)*cos(latitude*pi()/180)* pow(sin( ({$longitude}*pi()/180-longitude*pi()/180)/2),2)))*1000) AS distance";
                        $mdorder = Db::raw("({$longitude}-longitude)*({$longitude}-longitude) + ({$latitude}-latitude)*({$latitude}-latitude) asc");
                    }else{
                        $mdfield .= ",0 distance";
                        $mdorder = 'sort desc,id asc';
                    }
                    $mendian = [];
                    $bindmendianIds = [];
                    $bindmendianlist = Db::name('mendian')->field($mdfield)->where($mdwhere)->orderRaw($mdorder)->select()->toArray();
                    if(empty($bindmendianlist)) $bindmendianlist = [];
                    foreach ($bindmendianlist as $mdkey=>$bindmendian){
                        if(!$bindmendian['distance']){
                            $bindmendianlist[$mdkey]['distance'] = '';
                        }elseif($bindmendian['distance']<1000){
                            $bindmendianlist[$mdkey]['distance'] = round($bindmendian['distance'],2).'m';
                        }else{
                            $bindmendianlist[$mdkey]['distance'] = round($bindmendian['distance']/1000,1).'km';
                        }
                        if($mendian_id && $bindmendian['id']==$mendian_id){
                            $mendian = $bindmendianlist[$mdkey];
                        }
                        $bindmendianIds[] = $bindmendian['id'];
                    }

                    if($mendian && empty($mendian['pic'])){
                        $mendian['pic'] = PRE_URL.'/static/img/location/mendian.png';
                    }
                }
                if((empty($mendian_id) || empty($mendian)) && $bindmendianlist){
                    $mendian = $bindmendianlist[0];
                }
                if($mendian){
                    $mendian['address'] = $mendian['address']??'';
                    $mendian['area'] = $mendian['area']??'';
                }
            }
        }
        $commentposition = 0;//系统默认显示位置
        if(getcustom('product_comment')){
            $commentposition = 1;//商品详情的最底部
        }
        /*$custom['product_detail_header_hide'] = false;
        if(getcustom('product_sellprice_show')){
            $custom['product_detail_header_hide'] = true;
        }*/
        $showHeaderPic = true;
        if(getcustom('product_detail_special') && $shopset['show_header_pic']==0){
            $showHeaderPic = false;
        }
        $shopset['show_header_pic'] = $showHeaderPic;
        $custom['product_xunjia_btn'] = false;
        if(getcustom('product_xunjia_btn')){
            $custom['product_xunjia_btn'] = true;
        }
        $product['shop_yuding'] = $shop_yuding;

        if(getcustom('yx_invite_cashback')) {
            //处理邀请返现文字提示
            $deal_ictips       = \app\custom\OrderCustom::deal_ictips(aid,mid,0,[$product]);
            $product['ictips'] = $deal_ictips['ictips'];
        }

		//未登录查看价格
		if(getcustom('show_price_unlogin')){
			$mid = mid;
			if(!$mid && $shopset['is_show_price_unlogin'] == 0){
				$product['sell_price'] =  $shopset['show_price_unlogin_txt'];	
				$product['min_price'] = $shopset['show_price_unlogin_txt'];	
				$product['max_price'] = $shopset['show_price_unlogin_txt'];			
			}			
		}
		//未审核查看价格
		if(getcustom('show_price_uncheck')){
			if(mid && $this->member['checkst'] !=1 && $shopset['is_show_price_uncheck'] == 0){
				$product['sell_price'] =  $shopset['show_price_uncheck_txt'];	
				$product['min_price'] = $shopset['show_price_uncheck_txt'];	
				$product['max_price'] = $shopset['show_price_uncheck_txt'];	
			}			
		}
		

		$rdata = [];
		$rdata['status'] = 1;
		$rdata['show_money_price'] = $showMoneyPrice;
		$rdata['title'] = $product['name'];
		$rdata['shopset'] = $shopset;
		$rdata['sysset'] = $sysset;
		$rdata['isfavorite'] = $isfavorite;
		$rdata['product'] = $product;
		$rdata['business'] = $business;
		$rdata['commentlist'] = $commentlist;
		$rdata['commentcount'] = $commentcount;
		$rdata['cartnum'] = Db::name('shop_cart')->where('aid',aid)->where('mid',mid)->sum('num');
		$rdata['bboglist'] = $bboglist;
		$rdata['cuxiaolist'] = $newcxlist;
		$rdata['couponlist'] = $newcplist;
		$rdata['fuwulist'] = $fuwulist;
		$rdata['tjdatalist'] = $tjdatalist;
		$rdata['showjiesheng'] = 0;
		if(getcustom('showjiesheng') && $shopset['showcommission']==1 && $product['commission'] > 0){
			$rdata['showjiesheng'] = 1;
		}
		$rdata['is_member_auto_addlogin'] = 0;
		$rdata['need_login'] = 0;
		if(getcustom('member_auto_addlogin')){
			$is_member_auto_addlogin = Db::name('admin_set')->where('aid',aid)->value('is_member_auto_addlogin');
			if($is_member_auto_addlogin == 1){
				$rdata['is_member_auto_addlogin'] = 1;
				$mid = mid;
				if(!$mid){
					$rdata['need_login'] = 1;
				}
			}
		}
		$rdata['showtoptabbar'] = 0;
		if(getcustom('ngmm')){
			$rdata['showtoptabbar'] = 1;
			$rdata['shopset'] = Db::name('shop_sysset')->where('aid',aid)->field('showjd,comment,showcommission,hide_sales,hide_stock,show_lvupsavemoney,detail_guangao1,detail_guangao1_t,detail_guangao2,detail_guangao2_t')->find();
		}
		$rdata['showprice_dollar'] = $price_dollar;
		$rdata['xcx_scheme'] = $xcx_scheme;
        $rdata['mendian'] = $mendian??'';
        $rdata['bindmendianids'] = $bindmendianIds;
        $rdata['needlocation'] = $needlocation;
        $rdata['commentposition'] = $commentposition;
        $rdata['custom'] = $custom;
        $rdata['shopdetail_menudata'] = ['list'=>false];
		$designer_info = Db::name('designer_shopdetail')->where('aid',aid)->where('bid',$product['bid'])->find();
		if($designer_info){
			$shopdetail_menudata = json_decode($designer_info['menudata'],true);
			$rdata['shopdetail_menudata'] = $shopdetail_menudata;
		}
		if($product['bid']>0){
			$rs = Db::name('business_sysset')->where('aid',aid)->find();
			if($rs['show_shopdetail_menu'] == 0){
				$rdata['shopdetail_menudata'] = ['list'=>false];
			}
		}

		return $this->json($rdata);
	}
	//获取商品详情
	public function getproductdetail(){
		$proid = input('param.id/d');
		$where = [];
		$where[] = ['aid','=',aid];
		$where[] = ['id','=',$proid];
        $field = "bid,id,pic,name,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint,guigedata,status,ischecked,freighttype,freightdata,start_time,end_time,start_hours,end_hours,balance,limit_start,perlimitdan,commissionset,commissiondata1,commissiondata2,commissiondata3,commissionset4,price_type,product_type";
        if(getcustom('plug_tengrui')) {
            $field .= ',house_status,group_status,group_ids,is_rzh,relation_type';
        }
        if(getcustom('shop_other_infor')) {
            $field .= ',xunjia_text';
        }
        if(getcustom('shop_yuding')){
            $field .= ',yuding_stock';
        }
		if(getcustom('product_memberlevel_limit')){
			$field .= ',levellimitdata';
		}
		if(getcustom('product_wholesale')){
			$field .= ',jieti_discount_data,jieti_discount_type';
		}
		$product = Db::name('shop_product')->field($field)->where($where)->find();
		if(!$product){
			return $this->json(['status'=>0,'msg'=>'商品不存在']);
		}
		$product = $this->formatproduct($product);
	
		if($product['status']==0) return $this->json(['status'=>0,'msg'=>'商品已下架']);
		if($product['ischecked']!=1) return $this->json(['status'=>0,'msg'=>'商品未审核']);
		if($product['status']==2 && (strtotime($product['start_time']) > time() || strtotime($product['end_time']) < time())){
			return $this->json(['status'=>0,'msg'=>'商品未上架']);
		}
		if($product['status']==3){
			$start_time = strtotime(date('Y-m-d '.$product['start_hours']));
			$end_time = strtotime(date('Y-m-d '.$product['end_hours']));
			if(($start_time < $end_time && ($start_time > time() || $end_time < time())) || ($start_time >= $end_time && ($start_time > time() && $end_time < time()))){
				return $this->json(['status'=>0,'msg'=>'商品未上架']);
			}
		}


		if($product['status']==2 || $product['status']==3) $product['status']=1;

		$gglist = Db::name('shop_guige')->where('proid',$product['id'])->select()->toArray();
		$gglist = $this->formatgglist($gglist,$product['bid'],$product['lvprice']);

		$product['price_dollar'] =false;
		if(getcustom('price_dollar')){
			$usdrate = Db::name('shop_sysset')->where('aid',aid)->field('usdrate')->find();
			foreach($gglist as &$gg){
				if($usdrate['usdrate']>0){
					$gg['usdsell_price'] = round($gg['sell_price']/$usdrate['usdrate'],2);
				}
				$product['price_dollar']  = true;
			}
		}	
		//未登录查看价格
		if(getcustom('show_price_unlogin')){
			$shopset = Db::name('shop_sysset')->where('aid', aid)->find();
			$mid = mid;
			if(!$mid && $shopset['is_show_price_unlogin'] == 0){
				foreach($gglist as &$gg){
						$gg['sell_price'] = $shopset['show_price_unlogin_txt'];
				}
			}			
		}
		//未审核查看价格
		if(getcustom('show_price_uncheck')){
			$shopset = Db::name('shop_sysset')->where('aid', aid)->find();
			if(mid && $this->member['checkst'] !=1 && $shopset['is_show_price_uncheck'] == 0){	
				foreach($gglist as &$gg){
					$gg['sell_price'] = $shopset['show_price_uncheck_txt'];
				}
			}			
		}

        $sysset = Db::name('admin_set')->where('aid',aid)->field('name,logo,desc,fxjiesuantype,tel,kfurl,gzts,ddbb')->find();
       
        $shopset = Db::name('shop_sysset')->where('aid',aid)->field('showjd,comment,showcommission,hide_sales,hide_stock')->find();

		$guigelist = array();
		foreach($gglist as $k=>$v){
			if($product['balance'] > 0){
				$v['advance_price'] = round($v['sell_price'] * (1 - $product['balance']*0.01),2);
				$v['balance_price'] = round($v['sell_price'] * $product['balance']*0.01,2);
			}else{
				$v['balance_price'] = 0;
			}
            //预计佣金
            $commission = 0;
            $v['commission_desc'] = '元';
            $v['commission_desc_score'] = t('积分');
			//计算佣金
            if($this->member && $shopset['showcommission']==1){
                if($product['commissionset']!=-1){
                    $userlevel = Db::name('member_level')->where('aid',aid)->where('id',$this->member['levelid'])->find();
                    if($userlevel['can_agent']!=0){
                        if($product['commissionset']==1){//按比例
                            $commissiondata = json_decode($product['commissiondata1'],true);
                            if($commissiondata){
                                $commission = $commissiondata[$userlevel['id']]['commission1'] * ($v['sell_price'] - ($sysset['fxjiesuantype']==2 ? $v['cost_price'] : 0)) * 0.01;
                            }
                        }elseif($product['commissionset']==2){//按固定金额
                            $commissiondata = json_decode($product['commissiondata2'],true);
                            if($commissiondata){
                                $commission = $commissiondata[$userlevel['id']]['commission1'];
                            }
                        }elseif($product['commissionset']==3) {//提成是积分
                            $commissiondata = json_decode($product['commissiondata3'], true);
                            if ($commissiondata) {
                                $commissionScore = $commissiondata[$userlevel['id']]['commission1'];
                            }
                            //elseif($product['commissionset']==4 && $product['lvprice']==1)//按价格差
                            //    $lvprice_data = json_decode($v['lvprice_data'],true);
                            //    $commission = array_shift($lvprice_data) - $v['sell_price'];
                            //    if($commission < 0) $commission = 0;
                        }elseif($product['commissionset']==5){// 5 提成比例+积分
                            $commissiondata = json_decode($product['commissiondata1'],true);
                            if($commissiondata){
                                $commission = $commissiondata[$userlevel['id']]['commission1'] * ($v['sell_price'] - ($sysset['fxjiesuantype']==2 ? $v['cost_price'] : 0)) * 0.01;
                            }
                            $commissiondata = json_decode($product['commissiondata3'], true);
                            if ($commissiondata) {
                                $commissionScore = $commissiondata[$userlevel['id']]['commission1'];
                            }
                        }elseif($product['commissionset']==6){//提成金额+积分
                            $commissiondata = json_decode($product['commissiondata2'],true);
                            if($commissiondata){
                                $commission = $commissiondata[$userlevel['id']]['commission1'] *1;
                            }
                            $commissiondataScore = json_decode($product['commissiondata3'],true);
                            if($commissiondataScore){
                                $commissionScore = $commissiondataScore[$userlevel['id']]['commission1'];
                            }
                        }elseif($product['commissionset']==7){//分销按比例送积分
                            $commissiondata = json_decode($product['commissiondata4'],true);
                            if($commissiondata){
                                $commissionScore = $commissiondata[$userlevel['id']]['commission1'] * ($v['sell_price'] - ($sysset['fxjiesuantype']==2 ? $v['cost_price'] : 0)) * 0.01;
                            }
                        }elseif($product['commissionset']==0){//按会员等级
                            //fxjiesuantype 0按商品价格,1按成交价格,2按销售利润
                            if($userlevel['commissiontype']==1){ //固定金额按单
                                $commission = $userlevel['commission1'];
                            }else{
                                $commission = $userlevel['commission1'] * ($v['sell_price'] - ($sysset['fxjiesuantype']==2 ? $v['cost_price'] : 0)) * 0.01;
                            }
                        }
                    }
                }
                if($product['commissionset4']==1 && $product['lvprice']==1){ //极差分销
                    $lvprice_data = json_decode($v['lvprice_data'],true);
                    $commission += array_shift($lvprice_data) - $v['sell_price'];
                    if($commission < 0) $commission = 0;
                }
            }

            $v['commission'] = round($commission*100)/100;
            $v['commissionScore'] = dd_money_format($commissionScore,$this->score_weishu);

            //价格显示方式
            if(getcustom('price_show_type')){
                $price_show_type = Db::name('shop_sysset')->where('aid',aid)->value('price_show_type');
                $shopset['price_show_type'] = $price_show_type;
                if($this->member){
                    $userlevel = Db::name('member_level')->where('aid',aid)->where('id',$this->member['levelid'])->find();
                }else{
                    $userlevel = [];
                }
                $defalut_level = Db::name('member_level')->where('aid',aid)->order('id asc')->find();
                
                if(empty($userlevel) || $defalut_level['id'] == $this->member['levelid'] || $product['lvprice'] ==0){
                    $product['is_vip'] =0;
                }else{
                    $product['is_vip'] =1;
                }
                if(in_array($price_show_type,[1,2])){ //开启会员价
                    $lvprice_data =json_decode($product['lvprice_data'],true);
                    if($product['is_vip'] == 0){//不是会员 查询下个会员
                        $nextlevel = Db::name('member_level')->where('aid',aid)->where('sort','>',$userlevel['sort'])->order('sort,id')->find();
                        
                        if(empty($nextlevel) || $product['lvprice'] ==0 ){
                            $nextlevel = Db::name('member_level')->where('aid',aid)->where('sort','>',$defalut_level['sort'])->order('sort,id')->find();
                        }
                        $level_name = $nextlevel['name'];
                        $v['sell_price_origin'] = $lvprice_data[$nextlevel['id']];
                    }else{
                        if($userlevel && $product['lvprice'] ==1 ){
                            $level_name =  $userlevel['name'];
                        }
                    }
                    $v['level_name'] =  $level_name;
                }
            }
            $guigelist[$v['ks']] = $v;

        }
		$guigedata = json_decode($product['guigedata'],true);
		$product['guigedatanum'] = count($guigedata);
		$ggselected = [];
		foreach($guigedata as $v) {
			$ggselected[] = 0;
		}
		$ks = implode(',',$ggselected);
        $shop_yuding = false;
        if(getcustom('shop_yuding')){
            $shop_yuding = true;
        }
		$shopset['guigename'] = '';
		if(getcustom('product_guige_showtype')){
            $guige_name = Db::name('shop_sysset')->where('aid',aid)->value('guige_name');
		    $shopset['guige_name'] = $guige_name;
        }
        $product['shop_yuding'] = $shop_yuding;

		$product['limitdata'] = ['ismemberlevel_limit'=>false];
		if(getcustom('product_memberlevel_limit')){
			if($product['levellimitdata']){
				$rs = \app\model\ShopProduct::memberlevel_limit(aid,mid,$product,$this->member['levelid']);
				if($rs['status']==1){
					$product['limitdata'] = $rs['limitdata'];
				}
			}
		}

		return $this->json(['status'=>1,'product'=>$product,'guigelist'=>$guigelist,'guigedata'=>$guigedata,'ggselected'=>$ggselected,'ks'=>$ks,'shopset' => $shopset]);
	}
	//商品评价
	public function commentlist(){
		$proid = input('param.proid/d');
		$pagenum = input('post.pagenum');
		if(!$pagenum) $pagenum = 1;
		$pernum = 20;
		$where = [];
		$where[] = ['aid','=',aid];
		if($proid != 0){
			$where[] = ['proid','=',$proid];
		}else{
			$where[] = ['bid','=',0];
		}
		$where[] = ['status','=',1];
		$datalist = Db::name('shop_comment')->where($where)->page($pagenum,$pernum)->order('id desc')->select()->toArray();
		if(!$datalist) $datalist = [];
		foreach($datalist as $k=>$pl){
			$datalist[$k]['createtime'] = date('Y-m-d H:i',$pl['createtime']);
			if($datalist[$k]['content_pic']) $datalist[$k]['content_pic'] = explode(',',$datalist[$k]['content_pic']);
		}
        $addcommentbtn = false;
        if(getcustom('product_comment') && $pagenum==1){
            $product_comment = Db::name('shop_sysset')->where('aid',aid)->value('product_comment');
            $addcommentbtn = $product_comment?true:false;
        }
		if(request()->isPost()){
			return $this->json(['status'=>1,'data'=>$datalist,'addcommentbtn'=>$addcommentbtn]);
		}
		$rdata = [];
		$rdata['datalist'] = $datalist;
		$rdata['addcommentbtn'] = $addcommentbtn;
		return $this->json($rdata);
	}
	//商品海报
	function getposter(){
		$this->checklogin();
		$post = input('post.');
		$platform = platform;
		$page = '/pages/shop/product';
		$scene = 'id_'.$post['proid'].'-pid_'.$this->member['id'];
		//if($platform == 'mp' || $platform == 'h5' || $platform == 'app'){
		//	$page = PRE_URL .'/h5/'.aid.'.html#'. $page;
		//}
		$posterset = Db::name('admin_set_poster')->where('aid',aid)->where('type','product')->where('platform',$platform)->order('id')->find();

//		$posterdata = Db::name('member_poster')->where('aid',aid)->where('mid',mid)->where('scene',$scene)->where('type','product')->where('posterid',$posterset['id'])->find();
		//关闭缓存
		if(true || !$posterdata){
			$product = Db::name('shop_product')->where('id',$post['proid'])->find();
			//$product = $this->formatproduct($product);
			$sysset = Db::name('admin_set')->where('aid',aid)->find();
			$textReplaceArr = [
				'[头像]'=>$this->member['headimg'],
				'[昵称]'=>$this->member['nickname'],
				'[姓名]'=>$this->member['realname'],
				'[手机号]'=>$this->member['mobile'],
				'[商城名称]'=>$sysset['name'],
				'[商品名称]'=>$product['name'],
				'[商品销售价]'=>$product['sell_price'],
				'[商品市场价]'=>$product['market_price'],
				'[商品图片]'=>$product['pic'],
			];

			$poster = $this->_getposter(aid,$platform,$posterset['content'],$page,$scene,$textReplaceArr);
			$posterdata = [];
			$posterdata['aid'] = aid;
			$posterdata['mid'] = $this->member['id'];
			$posterdata['scene'] = $scene;
			$posterdata['page'] = $page;
			$posterdata['type'] = 'product';
			$posterdata['poster'] = $poster;
			$posterdata['posterid'] = $posterset['id'];
			$posterdata['createtime'] = time();
//			Db::name('member_poster')->insert($posterdata);
		}
		return $this->json(['status'=>1,'poster'=>$posterdata['poster']]);
	}
	//购物车 
	public function cart(){
		//$this->checklogin();
		$gwcdata = [];
		$field = 'id,bid,proid,ggid,num';
		if(getcustom('product_glass')){
		    $field .= ',glass_record_id';
        }
		if(input('param.bid')){
			$cartlist = Db::name('shop_cart')->field($field)->where('aid',aid)->where('bid',input('param.bid'))->where('mid',mid)->order('createtime desc')->select()->toArray();
		}else{
			$cartlist = Db::name('shop_cart')->field($field)->where('aid',aid)->where('mid',mid)->order('createtime desc')->select()->toArray();
		}
		if(!$cartlist) $cartlist = [];
		$newcartlist = [];
		foreach($cartlist as $k=>$gwc){
			if($newcartlist[$gwc['bid']]){
				$newcartlist[$gwc['bid']][] = $gwc;
			}else{
				$newcartlist[$gwc['bid']] = [$gwc];
			}
		}
        $sysset = Db::name('admin_set')->where('aid',aid)->find();
		foreach($newcartlist as $bid=>$gwclist){
			if($bid == 0){
				$business = [
                    'id'=>$sysset['id'],
                    'name'=>$sysset['name'],
                    'logo'=>$sysset['logo'],
                    'tel'=>$sysset['tel']
                ];
			}else{
				$business = Db::name('business')->where('aid',aid)->where('id',$bid)->field('id,name,logo,tel')->find();
			}
			$prolist = [];
			foreach($gwclist as $gwc){
				$product = Db::name('shop_product')->where('aid',aid)->where('status','<>',0)->where('id',$gwc['proid'])->find();
				if(!$product){
					Db::name('shop_cart')->where('aid',aid)->where('proid',$gwc['proid'])->delete();continue;
				}
				$guige = Db::name('shop_guige')->where('id',$gwc['ggid'])->find();
				if(!$guige){
					Db::name('shop_cart')->where('aid',aid)->where('ggid',$gwc['ggid'])->delete();continue;
				}
                $guige = $this->formatguige($guige,$product['bid'],$product['lvprice']);
				if($product['perlimitdan'] > 0 && $gwc['num'] > $product['perlimitdan']){
					$gwc['num'] = $product['perlimitdan'];
					Db::name('shop_cart')->where('aid',aid)->where('id',$gwc['id'])->update(['num'=>$gwc['num']]);
				}
				if(getcustom('product_wholesale') && $product['product_type'] == 4){
					$jieti_num = $gwc['num'];
					$jieti_discount_type = $product['jieti_discount_type'];
					if($jieti_discount_type == 0){
						$jieti_num = Db::name('shop_cart')->where('mid',mid)->where('proid',$product['id'])->sum('num');
					}
					
					$guige = $this->formatguigewholesale($guige,$product,$jieti_num);
				}
				$tmpitem = ['id'=>$gwc['id'],'checked'=>true,'product'=>$product,'guige'=>$guige,'num'=>$gwc['num']];
				if(getcustom('product_glass')){
				    $glassrecord = '';
				    if($gwc['glass_record_id']){
				        $glassrecord = Db::name('glass_record')->where('aid',aid)->where('mid',$this->mid)->where('id',$gwc['glass_record_id'])->find();
                    }
                    $tmpitem['glassrecord'] = $glassrecord??'';
                }
				$prolist[] = $tmpitem;
			}
			$newcartlist[$bid] = ['bid'=>$bid,'checked'=>true,'business'=>$business,'prolist'=>$prolist];
		}
		
		$shopset = Db::name('shop_sysset')->where('aid',aid)->field('gwctj')->find();
		if($shopset['gwctj']){
			$tjwhere = [];
			$tjwhere[] = ['aid','=',aid];
			$tjwhere[] = ['status','=',1];
			$tjwhere[] = ['ischecked','=',1];
			if(isdouyin == 1){
				$tjwhere[] = ['douyin_product_id','<>',''];
			}else{
				$tjwhere[] = ['douyin_product_id','=',''];
			}
			$where2 = "find_in_set('-1',showtj)";
			if($this->member){
				$where2 .= " or find_in_set('".$this->member['levelid']."',showtj)";
				if($this->member['subscribe']==1){
					$where2 .= " or find_in_set('0',showtj)";
				}
			}
			$tjwhere[] = Db::raw($where2);

			if(input('param.bid')){
				$tjwhere[] = ['bid','=',input('param.bid/d')];
			}else{
                if(getcustom('show_location')){
                    //定位模式:仅显示区域限制内的商品
                    $area = input('param.area');
                    $longitude = input('param.longitude');
                    $latitude = input('param.latitude');
                    if($sysset['mode']==2){
                        $b_where = [];
                        $b_where[] = ['aid','=',aid];
                        $b_where[] = ['status','=',1];
                        $b_where[] = ['is_open','=',1];
                        if($sysset['loc_range_type']==1){
                            if($latitude && $longitude){
                                $b_distance = $sysset['loc_range']?$sysset['loc_range']*1000:0;
                                $b_having = "round(6378.138*2*asin(sqrt(pow(sin( ({$latitude}*pi()/180-latitude*pi()/180)/2),2)+cos({$latitude}*pi()/180)*cos(latitude*pi()/180)* pow(sin( ({$longitude}*pi()/180-longitude*pi()/180)/2),2)))*1000) <={$b_distance}";
                                $limitBusiness = Db::name('business')->having($b_having)->where($b_where)->select()->toArray();
                                if($limitBusiness){
                                    $limitBids = array_column($limitBusiness,'id');
                                    $limitBids[] = 0;//追加平台0
                                    $tjwhere[] = ['bid','in',$limitBids];
                                }else{
                                    $tjwhere[] = ['bid','=',0];//仅显示平台商品
                                }
                            }
                        }else{
                            //同城
                            if($area){
                                //取省或者市
                                $areaArr = explode(',',$area);
                                $areaKey = min(count($areaArr)-1,1);//最小范围取到市
                                $areaName = $areaArr[$areaKey];
                                $b_where[] = ['city','=',$areaName];
                                $limitBids = Db::name('business')->where($b_where)->column('id');
                                if($limitBids){
                                    $limitBids[] = 0;
                                    $tjwhere[] = ['bid','in',$limitBids];
                                }else{
                                    $tjwhere[] = ['bid','=',0];//仅显示平台商品
                                }
                            }
                        }
                    }elseif ($sysset['mode']==3){
                        if(getcustom('product_bind_mendian')){
                            $mendian_id = input('param.mendian_id/d',0);
                            if($mendian_id){
                                $tjwhere[] = Db::raw("find_in_set({$mendian_id},`bind_mendian_ids`) OR find_in_set('-1',`bind_mendian_ids`) OR ISNULL(bind_mendian_ids)");
                            }
                        }
                    }
                    //定位模式
                }
				$business_sysset = Db::name('business_sysset')->where('aid',aid)->find();
				if(!$business_sysset || $business_sysset['status']==0 || $business_sysset['product_isshow']==0){
					$tjwhere[] = ['bid','=',0];
				}
			}
            $tjfields = "id,pic,name,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint,price_type";
            if(getcustom('product_cost_show')){
                $tjfields .= ',cost_price';
            }
			$tjdatalist = Db::name('shop_product')->field($tjfields)->where($tjwhere)->limit(12)->order(Db::raw('rand()'))->select()->toArray();
			if(!$tjdatalist) $tjdatalist = array();
			$tjdatalist = $this->formatprolist($tjdatalist);
		}else{
			$tjdatalist = [];
		}
		$rdata = [];
		$rdata['cartlist'] = array_values($newcartlist);
        if(getcustom('xixie')){
            if(!input('param.bid')){
                $xx_list = Db::name('xixie_cart')->where('mid',mid)->where('aid',aid)->field('id,bid,proid,num')->order('createtime desc')->select()->toArray();

                $xx_newlist = [];
                foreach($xx_list as $xcv){
                    if($xx_newlist[$xcv['bid']]){
                        $xx_newlist[$xcv['bid']][] = $xcv;
                    }else{
                        $xx_newlist[$xcv['bid']] = [$xcv];
                    }
                }
                foreach($xx_newlist as $xx_bid=>$xnv){
                    $business = Db::name('admin_set')->where('aid',aid)->field('id,name,logo,tel')->find();
                    if($business){
                        $business['name'] .= ' 洗鞋';
                    }
                    $prolist = [];
                    foreach($xnv as $xv){
                        $product = Db::name('xixie_product')->where('id',$xv['proid'])->where('aid',aid)->where('status','<>',0)->find();
                        if(!$product){
                            Db::name('xixie_cart')->where('proid',$xv['proid'])->where('aid',aid)->delete();continue;
                        }
                        if($this->member['is_vip'] && $product['vip_price']>0){
                            $product['sell_price'] = $product['vip_price'];
                        }
                        if($product['buymax'] > 0 && $xv['num'] > $product['buymax']){
                            $xv['num'] = $product['buymax'];
                            Db::name('xixie_cart')->where('aid',aid)->where('id',$xv['id'])->update(['num'=>$xv['num']]);
                        }
                        $prolist[] = ['id'=>$xv['id'],'checked'=>true,'product'=>$product,'num'=>$xv['num'],'type'=>2];
                    }
                    $xx_newlist[$xx_bid] = ['bid'=>$xx_bid,'checked'=>true,'business'=>$business,'prolist'=>$prolist];
                }
            }else{
                $xx_newlist = '';
            }
            $rdata['xixie'] = true;
            $rdata['xixie_cartlist'] = $xx_newlist?array_values($xx_newlist):'';
        }
		$rdata['tjdatalist'] = $tjdatalist;
		return $this->json($rdata);
	}
	public function formatguigewholesale($guige, $product,$num){
		if(!$this->member) return $guige;
		if(empty($product['jieti_discount_data'])) return $guige;
		$jieti_discount_data = json_decode($product['jieti_discount_data'],true);
		$sell_price = $guige['sell_price'];
		foreach($jieti_discount_data as $k=>$v){			
			if($num>=$v['start_num'] && $v['ratio']>0){
				$guige['sell_price'] = round($sell_price*$v['ratio']*0.01,2);
			}			
		}
		return $guige;
	}
	public function addcart(){
		$this->checklogin();
		$post = input('post.');
		$oldnum = 0;
		$num = intval($post['num']);
		
		$product = Db::name('shop_product')->where('aid',aid)->where('status','<>',0)->where('ischecked',1)->where('id',$post['proid'])->find();
		if(!$product) return $this->json(['status'=>0,'msg'=>'产品不存在或已下架']);
		if($product['freighttype']==3 || $product['freighttype']==4) return $this->json(['status'=>0,'msg'=>'虚拟商品不能加入购物车']);
		if(!$post['ggid']){
			if($num > 0){
				$post['ggid'] = Db::name('shop_guige')->where('proid',$post['proid'])->value('id');
			}else{
				$post['ggid'] = Db::name('shop_cart')->where('aid',aid)->where('mid',mid)->where('proid',$post['proid'])->order('id desc')->value('ggid');
			}
		}

		$gwc = Db::name('shop_cart')->where('aid',aid)->where('mid',mid)->where('proid',$post['proid'])->where('ggid',$post['ggid'])->find();
		if($gwc) $oldnum = $gwc['num'];
		if($num > 0 && $product['limit_start'] > 0 && $oldnum + $num < $product['limit_start']){
			$num = $product['limit_start'];
		}
		if($num > 0 && $product['perlimitdan'] > 0 && $oldnum + $num > $product['perlimitdan']){
			return $this->json(['status'=>0,'msg'=>'每单限购'.$product['perlimitdan'].'件']);
			$num = $product['perlimitdan'];
		}

		if(getcustom('product_memberlevel_limit')){
			if($product['levellimitdata']){
				$rs = \app\model\ShopProduct::memberlevel_limit(aid,mid,$product,$this->member['levelid']);
				if($rs['status']==1 && $rs['limitdata']['ismemberlevel_limit']){
					return $this->json(['status'=>0,'msg'=>'该商品'.$rs['limitdata']['days'].'天内限购'.$rs['limitdata']['limit_num'].'件']);
				}
			}
		}



        if(getcustom('plug_tengrui')) {
            //判断是否是否符合会员认证、会员关系、一户、用户组
            $tr_check = new \app\common\TengRuiCheck();
            $check_product = $tr_check->check_product($this->member,$product);
            if($check_product && $check_product['status'] == 0){
                return $this->json(['status'=>$check_product['status'],'msg'=>$check_product['msg']]);
            }
            $tr_roomId = $check_product['tr_roomId'];
        }
		if($oldnum + $num <=0 || ($product['limit_start'] > 0 && $oldnum + $num < $product['limit_start'])){
			Db::name('shop_cart')->where('aid',aid)->where('mid',mid)->where('proid',$post['proid'])->where('ggid',$post['ggid'])->delete();
			$cartnum = Db::name('shop_cart')->where('aid',aid)->where('mid',mid)->sum('num');
			return $this->json(['status'=>1,'msg'=>'移除成功','cartnum'=>$cartnum]);
		}
		if($gwc){
		    $updata['createtime'] = time();
            if(getcustom('product_glass')){
                $updata['glass_record_id'] = $post['glass_record_id'];
            }
			Db::name('shop_cart')->where('aid',aid)->where('mid',mid)->where('proid',$post['proid'])->where('ggid',$post['ggid'])->inc('num',$num)->update($updata);
		}else{
			$data = [];
			$data['aid'] = aid;
			$data['bid'] = $product['bid'];
			$data['mid'] = mid;
			$data['ggid'] = $post['ggid'];
			$data['createtime'] = time();
			$data['proid'] = $post['proid'];
			$data['num'] = $num;
			if(getcustom('product_glass')){
			    $data['glass_record_id'] = $post['glass_record_id'];
            }
			Db::name('shop_cart')->insert($data);
		}
		$cartnum = Db::name('shop_cart')->where('aid',aid)->where('mid',mid)->sum('num');
		return $this->json(['status'=>1,'msg'=>'加入购物车成功','cartnum'=>$cartnum]);
	}
	public function addcartmore(){
		$this->checklogin();
		//$post = input('post.');
		$data = input('post.');
		$prodata = explode('-',$data['prodata']);
		foreach($prodata as $key=>$pro){
			$sdata = explode(',',$pro);
			$post['proid'] = $sdata[0];
			$post['ggid'] = $sdata[1];
			$post['num'] = $sdata[2];
			$post['glass_record_id'] = $sdata[2]??0;
		$oldnum = 0;
		$num = intval($post['num']);
		
		$product = Db::name('shop_product')->where('aid',aid)->where('status','<>',0)->where('ischecked',1)->where('id',$post['proid'])->find();
		if(!$product) return $this->json(['status'=>0,'msg'=>'产品不存在或已下架']);
		if($product['freighttype']==3 || $product['freighttype']==4) return $this->json(['status'=>0,'msg'=>'虚拟商品不能加入购物车']);
		if(!$post['ggid']){
			if($num > 0){
				$post['ggid'] = Db::name('shop_guige')->where('proid',$post['proid'])->value('id');
			}else{
				$post['ggid'] = Db::name('shop_cart')->where('aid',aid)->where('mid',mid)->where('proid',$post['proid'])->order('id desc')->value('ggid');
			}
		}

		$gwc = Db::name('shop_cart')->where('aid',aid)->where('mid',mid)->where('proid',$post['proid'])->where('ggid',$post['ggid'])->find();
		if($gwc) $oldnum = $gwc['num'];

		if($product['product_type'] == 4){
			if($product['jieti_discount_type']==1){
				if($num > 0 && $product['limit_start'] > 0 && $oldnum + $num < $product['limit_start']){
					$num = $product['limit_start'];
				}
			}
		}else{
			if($num > 0 && $product['limit_start'] > 0 && $oldnum + $num < $product['limit_start']){
				$num = $product['limit_start'];
			}
		}		
		if($num > 0 && $product['perlimitdan'] > 0 && $oldnum + $num > $product['perlimitdan']){
			return $this->json(['status'=>0,'msg'=>'每单限购'.$product['perlimitdan'].'件']);
			$num = $product['perlimitdan'];
		}

		if(getcustom('product_memberlevel_limit')){
			if($product['levellimitdata']){
				$rs = \app\model\ShopProduct::memberlevel_limit(aid,mid,$product,$this->member['levelid']);
				if($rs['status']==1 && $rs['limitdata']['ismemberlevel_limit']){
					return $this->json(['status'=>0,'msg'=>'该商品'.$rs['limitdata']['days'].'天内限购'.$rs['limitdata']['limit_num'].'件']);
				}
			}
		}



        if(getcustom('plug_tengrui')) {
            //判断是否是否符合会员认证、会员关系、一户、用户组
            $tr_check = new \app\common\TengRuiCheck();
            $check_product = $tr_check->check_product($this->member,$product);
            if($check_product && $check_product['status'] == 0){
                return $this->json(['status'=>$check_product['status'],'msg'=>$check_product['msg']]);
            }
            $tr_roomId = $check_product['tr_roomId'];
        }
		if($oldnum + $num <=0 || ($product['limit_start'] > 0 && $oldnum + $num < $product['limit_start'])){
			Db::name('shop_cart')->where('aid',aid)->where('mid',mid)->where('proid',$post['proid'])->where('ggid',$post['ggid'])->delete();
			$cartnum = Db::name('shop_cart')->where('aid',aid)->where('mid',mid)->sum('num');
			return $this->json(['status'=>1,'msg'=>'移除成功','cartnum'=>$cartnum]);
		}
		if($gwc){
		    $updata['createtime'] = time();
            if(getcustom('product_glass')){
                $updata['glass_record_id'] = $post['glass_record_id'];
            }
			Db::name('shop_cart')->where('aid',aid)->where('mid',mid)->where('proid',$post['proid'])->where('ggid',$post['ggid'])->inc('num',$num)->update($updata);
		}else{
			$data = [];
			$data['aid'] = aid;
			$data['bid'] = $product['bid'];
			$data['mid'] = mid;
			$data['ggid'] = $post['ggid'];
			$data['createtime'] = time();
			$data['proid'] = $post['proid'];
			$data['num'] = $num;
			if(getcustom('product_glass')){
			    $data['glass_record_id'] = $post['glass_record_id'];
            }
			Db::name('shop_cart')->insert($data);
		}
	}
		$cartnum = Db::name('shop_cart')->where('aid',aid)->where('mid',mid)->sum('num');
		return $this->json(['status'=>1,'msg'=>'加入购物车成功','cartnum'=>$cartnum]);
	}
	public function cartChangenum(){
		$this->checklogin();
		$id = input('post.id/d');
		$num = input('post.num/d');
        $type = input('post.type')?input('post.type'):'';
		if($num < 1) $num = 1;
        if(!$type){
    		$cart = Db::name('shop_cart')->where('aid',aid)->where('id',$id)->where('mid',mid)->find();
            $product = Db::name('shop_product')->where('aid',aid)->where('id',$cart['proid'])->find();
            $guige = Db::name('shop_guige')->where('aid',aid)->where('id',$cart['ggid'])->find();
            if($guige['limit_start'] > 0 && $num < $guige['limit_start']){
                return $this->json(['status'=>0,'msg'=>'该商品规格'.$guige['limit_start'].'件起售']);
            }
            if($product['limit_start'] > 0 && $num < $product['limit_start']){
                return $this->json(['status'=>0,'msg'=>'该商品'.$product['limit_start'].'件起售']);
            }
            if($product['perlimitdan'] > 0 && $num > $product['perlimitdan']){
                return $this->json(['status'=>0,'msg'=>'该商品每单限购'.$product['perlimitdan'].'件']);
            }
        }

		if(getcustom('product_memberlevel_limit')){
			if($product['levellimitdata']){
				$rs = \app\model\ShopProduct::memberlevel_limit(aid,mid,$product,$this->member['levelid']);
				if($rs['status']==1 && $rs['limitdata']['ismemberlevel_limit']){
					return $this->json(['status'=>0,'msg'=>'该商品'.$rs['limitdata']['days'].'天内限购'.$rs['limitdata']['limit_num'].'件']);
				}
			}
		}

        if(getcustom('xixie')){
            if($type == 2){
                $cart = Db::name('xixie_cart')->where('id',$id)->where('mid',mid)->where('aid',aid)->find();
                $product = Db::name('xixie_product')->where('id',$cart['proid'])->where('aid',aid)->find();
                if($product['buymax'] > 0 && $num > $product['buymax']){
                    return $this->json(['status'=>0,'msg'=>'该商品每单限购'.$product['buymax'].'件']);
                }
                $up = Db::name('xixie_cart')->where('id',$id)->where('mid',mid)->update(['num'=>$num]);
                if($up){
                    return $this->json(['status'=>1,'msg'=>'修改成功']);
                }else{
                    return $this->json(['status'=>0,'msg'=>'修改成功']);
                }
            }
        }
		Db::name('shop_cart')->where('id',$id)->where('mid',mid)->update(['num'=>$num]);
		return $this->json(['status'=>1,'msg'=>'修改成功']);
	}
	public function cartdelete(){
		$this->checklogin();
		$id = input('post.id/d');
        $type = input('post.type')?input('post.type'):'';
        if(!$type){
    		if(!$id){
    			$bid = input('post.bid/d');
    			Db::name('shop_cart')->where('bid',$bid)->where('mid',mid)->delete();
    			return $this->json(['status'=>1,'msg'=>'删除成功']);
    		}
        }
        if(getcustom('xixie')){
            if($type == 2){
                if($id){
                    //删除购物车
                    $del = Db::name('xixie_cart')->where('id',$id)->where('mid',mid)->delete();
                    if($del){
                        return $this->json(['status'=>1,'msg'=>'删除成功']);
                    }else{
                        return $this->json(['status'=>0,'msg'=>'删除失败']);
                    }
                }else{
                    //删除购物车
                    $del = Db::name('xixie_cart')->where('bid',0)->where('mid',mid)->delete();
                    if($del){
                        return $this->json(['status'=>1,'msg'=>'删除成功']);
                    }else{
                        return $this->json(['status'=>0,'msg'=>'删除失败']);
                    }
                    return $this->json(['status'=>0,'msg'=>'删除失败']);
                }
            }
        }
		Db::name('shop_cart')->where('id',$id)->where('mid',mid)->delete();
		return $this->json(['status'=>1,'msg'=>'删除成功']);
	}
	public function cartclear(){
		$bid = input('param.bid');
		if(!$bid) $bid = 0;
		Db::name('shop_cart')->where('aid',aid)->where('bid',$bid)->where('mid',mid)->delete();
		return $this->json(['status'=>1,'msg'=>'删除成功']);
	}
	//快速购买页
	public function fastbuy(){
		$bid = input('param.bid');
		if(!$bid) $bid = 0;
		$cid = input('param.cid');
		if(!$cid) $cid = 0;
		if($bid > 0){
			$clist = Db::name('shop_category2')->where('aid',aid)->where('bid',$bid)->where('pid',$cid)->where('status',1)->order('sort desc,id')->select()->toArray();
		}else{
            $where2 = "find_in_set('-1',showtj)";
            if($this->member){
                $where2 .= " or find_in_set('".$this->member['levelid']."',showtj)";
                if($this->member['subscribe']==1){
                    $where2 .= " or find_in_set('0',showtj)";
                }
            }
            $tjwhere[] = Db::raw($where2);
			$clist = Db::name('shop_category')->where($tjwhere)->where('aid',aid)->where('pid',$cid)->where('status',1)->order('sort desc,id')->select()->toArray();
		}
		foreach($clist as $k=>$v){
			$where = [];
			$where[] = ['aid','=',aid];
			$where[] = ['bid','=',$bid];
			//$where[] = ['status','=',1];
			$where[] = ['ischecked','=',1];
			$where[] = ['freighttype','<',3];
			if(isdouyin == 1){
				$where[] = ['douyin_product_id','<>',''];
			}else{
				$where[] = ['douyin_product_id','=',''];
			}
			$nowtime = time();
			$nowhm = date('H:i');
			$where[] = Db::raw("`status`=1 or (`status`=2 and unix_timestamp(start_time)<=$nowtime and unix_timestamp(end_time)>=$nowtime) or (`status`=3 and ((start_hours<end_hours and start_hours<='$nowhm' and end_hours>='$nowhm') or (start_hours>=end_hours and (start_hours<='$nowhm' or end_hours>='$nowhm'))) )");

			//子分类 
			if($bid > 0){
				$childcid = db('shop_category2')->where(['aid'=>aid,'pid'=>$v['id']])->column('id');
				if($childcid){
					$child2cid = db('shop_category2')->where(['aid'=>aid,'pid'=>['in',$childcid]])->column('id');
					$cCate = array_merge($childcid, $child2cid, [$v['id']]);
					if($cCate){
						$whereCid = [];
						foreach($cCate as $c2){
							$whereCid[] = "find_in_set({$c2},cid2)";
						}
						$where[] = Db::raw(implode(' or ',$whereCid));
					}
				} else {
					$where[] = Db::raw("find_in_set(".$v['id'].",cid2)");
				}
			}else{
				$childcid = db('shop_category')->where($tjwhere)->where(['aid'=>aid,'pid'=>$v['id']])->column('id');
				if($childcid){
					$child2cid = db('shop_category')->where($tjwhere)->where(['aid'=>aid,'pid'=>['in',$childcid]])->column('id');
					$cCate = array_merge($childcid, $child2cid, [$v['id']]);
					if($cCate){
						$whereCid = [];
						foreach($cCate as $c2){
							$whereCid[] = "find_in_set({$c2},cid)";
						}
						$where[] = Db::raw(implode(' or ',$whereCid));
					}
				} else {
					$where[] = Db::raw("find_in_set(".$v['id'].",cid)");
				}
			}
			//是否可见
			$where2 = "find_in_set('-1',showtj)";
			if($this->member){
				$where2 .= " or find_in_set('".$this->member['levelid']."',showtj)";
				if($this->member['subscribe']==1){
					$where2 .= " or find_in_set('0',showtj)";
				}
			}else{
                $where2 .= " or find_in_set('-2',showtj)";
            }
			$where[] = Db::raw($where2);

			$prolist = Db::name('shop_product')->field("pic,id,name,stock,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint,guigedata,limit_start,price_type,product_type")->where($where)->order('sort desc,id desc')->select()->toArray();
			if(!$prolist) $prolist = [];
			foreach($prolist as $k2=>$v2){
				if(getcustom('product_wholesale') && $v2['product_type'] == 4){
					$guigedata = json_decode($v2['guigedata'],true);
					$prolist[$k]['gg_num'] =  count($guigedata);
				}
			}
			$prolist = $this->formatprolist($prolist);
			//未登录查看价格
			if(getcustom('show_price_unlogin')){
				$shopset = Db::name('shop_sysset')->where('aid', aid)->find();
				$mid = mid;
				if(!$mid && $shopset['is_show_price_unlogin'] == 0){
					foreach($prolist as &$pv){
						$pv['sell_price'] =  $shopset['show_price_unlogin_txt'];					
					}
				}			
			}
			//未审核查看价格
			if(getcustom('show_price_uncheck')){
				$shopset = Db::name('shop_sysset')->where('aid', aid)->find();
				if(mid && $this->member['checkst'] !=1 && $shopset['is_show_price_uncheck'] == 0){
					foreach($prolist as &$pv){
						$pv['sell_price'] =  $shopset['show_price_uncheck_txt'];					
					}
				}			
			}
			if(!$prolist){
				unset($clist[$k]);
			}else{
				$clist[$k]['prolist'] = $prolist;
			}
		}
		$clist = array_values($clist);

		$list = Db::name('shop_cart')->where('aid',aid)->where('bid',$bid)->where('mid',mid)->order('createtime desc')->select()->toArray();
		$total = 0;
		$totalprice = 0;
		foreach($list as $k=>$v){
			$product = Db::name('shop_product')->field('pic,bid,id,name,stock,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint,guigedata,limit_start')->where('id',$v['proid'])->find();
			if(!$product){
				unset($list[$k]);
				Db::name('shop_cart')->where('id',$v['id'])->delete();
				continue;
			}
			$product = $this->formatproduct($product);
			$guige = Db::name('shop_guige')->where('id',$v['ggid'])->find();
			if(!$guige){
				unset($list[$k]);
				Db::name('shop_cart')->where('id',$v['id'])->delete();
				continue;
			}
            $guige = $this->formatguige($guige,$product['bid'],$product['lvprice']);
			$list[$k]['product'] = $product;
			$list[$k]['guige'] = $guige;
			$total += $v['num'];
			$totalprice += $guige['sell_price'] * $v['num'];
		}
		$totalprice = number_format($totalprice,2,'.','');
		$cartList = ['list'=>$list,'total'=>$total,'totalprice'=>$totalprice];
		$numtotal = [];
		foreach($clist as $i=>$v){
			foreach($v['prolist'] as $j=>$pro){
				$numtotal[$pro['id']] = 0;
			}
		}
		foreach($cartList['list'] as $i=>$v){
			$numtotal[$v['proid']] += $v['num'];
		}

		$rdata = [];
		$rdata['status'] = 1;
		$rdata['data'] = $clist;
		$rdata['cartList'] = $cartList;
		$rdata['numtotal'] = $numtotal;
		return $this->json($rdata);
	}
	
	//快速购买页2
	public function fastbuy2(){
		$bid = input('param.bid');
		if(!$bid || $bid=='undefined') $bid = 0;
		if($bid!=0){
			$business = Db::name('business')->where('aid',aid)->where('id',$bid)->field('id,name,logo,content,pics,desc,tel,address,sales,start_hours,end_hours,latitude,longitude,comment_num,comment_score,comment_haopercent')->find();
			$business['pic'] = explode(',',$business['pics'])[0];
		}else{
			$business = Db::name('admin_set')->where('aid',aid)->field('id,name,logo,pics,`desc` content,tel,address,latitude,longitude')->find();
            //查询图片
            $shopset = Db::name('shop_sysset')->where('aid',aid)->find();
            $business['pic'] =  $shopset['fastbuy_toppic']?$shopset['fastbuy_toppic']:'';
			$comment_num = 0 + Db::name('shop_comment')->where('aid',aid)->where('bid',0)->where('status',1)->count();
			
			$haonum = 0 + Db::name('shop_comment')->where('aid',aid)->where('bid',0)->where('status',1)->where('score','>',3)->count(); //好评数
			if($comment_num > 0){
				$haopercent = round($haonum/$comment_num*100,2);
			}else{
				$haopercent = 100;
			}
			$business['comment_num'] = $comment_num;
			$business['comment_haopercent'] = $haopercent;
		}

		$cid = input('param.cid');
		if(!$cid) $cid = 0;
		
		if($bid > 0){
			$clist = Db::name('shop_category2')->where('aid',aid)->where('bid',$bid)->where('pid',$cid)->where('status',1)->order('sort desc,id')->select()->toArray();
		}else{
            $where2 = "find_in_set('-1',showtj)";
            if($this->member){
                $where2 .= " or find_in_set('".$this->member['levelid']."',showtj)";
                if($this->member['subscribe']==1){
                    $where2 .= " or find_in_set('0',showtj)";
                }
            }
            $tjwhere[] = Db::raw($where2);
			$clist = Db::name('shop_category')->where($tjwhere)->where('aid',aid)->where('pid',$cid)->where('status',1)->order('sort desc,id')->select()->toArray();
		}
		foreach($clist as $k=>$v){
			$where = [];
			$where[] = ['aid','=',aid];
			$where[] = ['bid','=',$bid];
			//$where[] = ['status','=',1];
			$where[] = ['ischecked','=',1];
			$where[] = ['freighttype','<',3];
			if(isdouyin == 1){
				$where[] = ['douyin_product_id','<>',''];
			}else{
				$where[] = ['douyin_product_id','=',''];
			}
			$nowtime = time();
			$nowhm = date('H:i');
			$where[] = Db::raw("`status`=1 or (`status`=2 and unix_timestamp(start_time)<=$nowtime and unix_timestamp(end_time)>=$nowtime) or (`status`=3 and ((start_hours<end_hours and start_hours<='$nowhm' and end_hours>='$nowhm') or (start_hours>=end_hours and (start_hours<='$nowhm' or end_hours>='$nowhm'))) )");

			//子分类 
			if($bid > 0){
				$childcid = db('shop_category2')->where(['aid'=>aid,'pid'=>$v['id']])->column('id');
				if($childcid){
					$child2cid = db('shop_category2')->where(['aid'=>aid,'pid'=>['in',$childcid]])->column('id');
					$cCate = array_merge($childcid, $child2cid, [$v['id']]);
					if($cCate){
						$whereCid = [];
						foreach($cCate as $c2){
							$whereCid[] = "find_in_set({$c2},cid2)";
						}
						$where[] = Db::raw(implode(' or ',$whereCid));
					}
				} else {
					$where[] = Db::raw("find_in_set(".$v['id'].",cid2)");
				}
			}else{
				$childcid = db('shop_category')->where($tjwhere)->where(['aid'=>aid,'pid'=>$v['id']])->column('id');
				if($childcid){
					$child2cid = db('shop_category')->where($tjwhere)->where(['aid'=>aid,'pid'=>['in',$childcid]])->column('id');
					$cCate = array_merge($childcid, $child2cid, [$v['id']]);
					if($cCate){
						$whereCid = [];
						foreach($cCate as $c2){
							$whereCid[] = "find_in_set({$c2},cid)";
						}
						$where[] = Db::raw(implode(' or ',$whereCid));
					}
				} else {
					$where[] = Db::raw("find_in_set(".$v['id'].",cid)");
				}
			
			}
			//是否可见
			$where2 = "find_in_set('-1',showtj)";
			if($this->member){
				$where2 .= " or find_in_set('".$this->member['levelid']."',showtj)";
				if($this->member['subscribe']==1){
					$where2 .= " or find_in_set('0',showtj)";
				}
			}else{
                $where2 .= " or find_in_set('-2',showtj)";
            }
			$where[] = Db::raw($where2);

			$prolist = Db::name('shop_product')->field("pic,id,name,stock,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint,guigedata,price_type,product_type")->where($where)->order('sort desc,id desc')->select()->toArray();
			if(!$prolist) $prolist = [];
			$prolist = $this->formatprolist($prolist);
			if(!$prolist){
				unset($clist[$k]);
			}else{
				foreach($prolist as $k2=>$v2){
					$gglist = Db::name('shop_guige')->where('proid',$v2['id'])->select()->toArray();
					$prolist[$k2]['gglist'] = $gglist;
					$prolist[$k2]['ggcount'] = count($gglist);
					if($v2['limit_start']==0) $v2['limit_start'] = 1;
					if($v2['limit_per']==0) $v2['limit_per'] = 999999;
					if(getcustom('product_wholesale') && $v2['product_type'] == 4){
						$guigedata = json_decode($v2['guigedata'],true);
						$prolist[$k]['gg_num'] =  count($guigedata);
					}
					
				}
				//未登录查看价格
				if(getcustom('show_price_unlogin')){
					$shopset = Db::name('shop_sysset')->where('aid', aid)->find();
					$mid = mid;
					if(!$mid && $shopset['is_show_price_unlogin'] == 0){
						foreach($prolist as &$pv){
							$pv['sell_price'] =  $shopset['show_price_unlogin_txt'];					
						}
					}			
				}
				//未审核查看价格
				if(getcustom('show_price_uncheck')){
					$shopset = Db::name('shop_sysset')->where('aid', aid)->find();
					if(mid && $this->member['checkst'] !=1 && $shopset['is_show_price_uncheck'] == 0){
						foreach($prolist as &$pv){
							$pv['sell_price'] =  $shopset['show_price_uncheck_txt'];					
						}
					}			
				}
				$clist[$k]['prolist'] = $prolist;
			}
		}
		$clist = array_values($clist);

		$list = Db::name('shop_cart')->where('aid',aid)->where('bid',$bid)->where('mid',mid)->order('createtime desc')->select()->toArray();
		$total = 0;
		$totalprice = 0;
		foreach($list as $k=>$v){
			$product = Db::name('shop_product')->field('pic,bid,id,name,stock,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint,guigedata')->where('id',$v['proid'])->find();
			if(!$product){
				unset($list[$k]);
				Db::name('shop_cart')->where('id',$v['id'])->delete();
				continue;
			}
			$product = $this->formatproduct($product);
			$guige = Db::name('shop_guige')->where('id',$v['ggid'])->find();
			if(!$guige){
				unset($list[$k]);
				Db::name('shop_cart')->where('id',$v['id'])->delete();
				continue;
			}
            $guige = $this->formatguige($guige,$product['bid'],$product['lvprice']);
			$list[$k]['product'] = $product;
			$list[$k]['guige'] = $guige;
			$total += $v['num'];
			$totalprice += $guige['sell_price'] * $v['num'];
		}
		$totalprice = number_format($totalprice,2,'.','');
		$cartList = ['list'=>$list,'total'=>$total,'totalprice'=>$totalprice,'leftprice'=>0];
		$numtotal = [];
		foreach($clist as $i=>$v){
			foreach($v['prolist'] as $j=>$pro){
				$numtotal[$pro['id']] = 0;
			}
		}
		foreach($cartList['list'] as $i=>$v){
			$numtotal[$v['proid']] += $v['num'];
		}

		if(getcustom('plug_businessqr')){
			$paylist = Db::name('plug_businessqr_pay')->where('aid',aid)->where('bid',$bid)->where('status',1)->order('sort desc,id desc')->select()->toArray();
            $menu_list = cache('business_menu_list_'.aid);
            if(empty($menu_list)) {
                $menu_list = [
                    [
                        'name' => '会员支付',
                        'alias' => '',
                        'sort' => 1,
                        'st' => 3,
                    ],
                    [
                        'name' => '商家信息',
                        'alias' => '',
                        'sort' => 2,
                        'st' => 1,
                    ],
                    [
                        'name' => '商品',
                        'alias' => '',
                        'sort' => 3,
                        'st' => 0,
                    ],
                ];
            }
		}else{
			$paylist = [];
		}

		$rdata = [];
		$rdata['status'] = 1;
		$rdata['data'] = $clist;
		$rdata['paylist'] = $paylist;
		$rdata['cartList'] = $cartList;
		$rdata['numtotal'] = $numtotal;
		$rdata['business'] = $business;
        $rdata['menuList'] = $menu_list ? $menu_list : [];
		$rdata['bid'] = $bid;
		return $this->json($rdata);
	}
	//获取促销信息
	public function getcuxiaoinfo(){
		$id = input('post.id');
        $multi_promotion = 0;
        if(getcustom('multi_promotion')){
            $multi_promotion = 1;
        }

        if($multi_promotion && is_array($id)) {
            $list = Db::name('cuxiao')->whereIn('id',$id)->where('aid',aid)->select();
            if(!$list){
                return $this->json(['status'=>0,'msg'=>'获取失败']);
            }

            if($list) {
                foreach ($list as $key => $info) {
                    $proinfo[$key] = false;
                    $gginfo[$key] = false;
                    if(($info['type'] == 2 || $info['type'] == 3) && $info['proid']){
                        $proinfo[$key] = Db::name('shop_product')->field('id,name,pic,sell_price')->where('aid',aid)->where('id',$info['proid'])->find();
                        $gginfo[$key] = Db::name('shop_guige')->where('aid',aid)->where('id',$info['ggid'])->find();
                    }
                }
            }

            return $this->json(['status'=>1,'info'=>$list,'product'=>$proinfo,'guige'=>$gginfo]);
        } else {
            $info = Db::name('cuxiao')->where('id',$id)->where('aid',aid)->find();
            if(!$info){
                return $this->json(['status'=>0,'msg'=>'获取失败']);
            }

            $proinfo = false;
            $gginfo = false;
            if(($info['type'] == 2 || $info['type'] == 3) && $info['proid']){
                $proinfo = Db::name('shop_product')->field('id,name,pic,sell_price')->where('aid',aid)->where('id',$info['proid'])->find();
                $gginfo = Db::name('shop_guige')->where('aid',aid)->where('id',$info['ggid'])->find();
            }
            return $this->json(['status'=>1,'info'=>$info,'product'=>$proinfo,'guige'=>$gginfo]);
        }

	}
	//订单提交页
	public function buy(){
		$this->checklogin();
		$prodata = explode('-',input('param.prodata'));
        $mendian_id = input('param.mendian_id/d',0);
		$multi_promotion = 0;
        if(getcustom('multi_promotion')){
            $multi_promotion = 1;
        }
		//未认证不可下单
		if(getcustom('show_price_uncheck')){
			$shopset = Db::name('shop_sysset')->where('aid', aid)->find();
			$mid = mid;
			if(!$mid && $shopset['is_show_price_uncheck'] == 0){
				$msg = $shopset['show_price_uncheck_txt'];
				return $this->json(['status'=>0,'msg'=>$msg]);
			}			
		}
		
		$userlevel = Db::name('member_level')->where('aid',aid)->where('id',$this->member['levelid'])->find();
		$adminset = Db::name('admin_set')->where('aid',aid)->find();
		$userinfo = [];
		$userinfo['discount'] = $userlevel['discount'];
		$userinfo['score'] = $this->member['score'];
		$userinfo['score2money'] = $adminset['score2money'];
        $userinfo['scoredk_money'] = round($userinfo['score'] * $userinfo['score2money'],2);
		$userinfo['scoredkmaxpercent'] = $adminset['scoredkmaxpercent'];
		$userinfo['scoremaxtype'] = 0; //0最大百分比 1最大抵扣金额
		$userinfo['realname'] = $this->member['realname'];
		$userinfo['tel'] = $this->member['tel'];
		$userinfo['money'] = $this->member['money'];

		$scoredkmaxmoney = 0;//积分最大抵扣金额
		$allbuydata = [];
		$autofahuo = 0;
        $cat_buynum=[];
        //满减活动
        $mjset = Db::name('manjian_set')->where('aid',aid)->find();
        if($mjset && $mjset['status']==1){
            $mjdata = json_decode($mjset['mjdata'],true);
            //指定分类
            if($mjset['fwtype']==1){
                //指定分类或商品分类不存在
                if(empty($mjset['categoryids'])){
                    $mjdata = array();
                }
            //指定商品
            }else if($mjset['fwtype']==2){
                if(empty($mjset['productids'])){
                    $mjdata = array();
                }
            }
            if(getcustom('plug_tengrui')) {
                if($mjdata){
                    $tr_check = new \app\common\TengRuiCheck();
                    //判断是否是否符合会员认证、会员关系、一户
                    $check_manjian = $tr_check->check_manjian($this->member,$mjset);
                    if($check_manjian && $check_manjian['status'] == 0){
                        $mjdata = array();
                    }
                }
            }
        }else{
            $mjdata = array();
        }
        $glassProductNum = 0;
		
		$business_selfscore = 0;
		if(getcustom('business_selfscore')){
            //可设置商户开启独立积分账户
			$bset = Db::name('business_sysset')->where('aid',aid)->find();
			if($bset['business_selfscore'] == 1 && $bset['business_selfscore2'] == 1){
				$business_selfscore = 1;
			}
			$scoredkdataArr = [];
		}
		$productMoneyPay = [];//不同支付方式，不允许一起结算
        $productLimit = [];
        $protypes=[];//存在的商品类型
		foreach($prodata as $key=>$gwc){
			list($proid,$ggid,$num) = explode(',',$gwc);
            if(getcustom('to86yk')){
                $field = "id,aid,bid,cid,pic,name,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint,freighttype,freightdata,perlimit,gettj,gettjtip,gettjurl,scoredkmaxset,scoredkmaxval,status,start_time,end_time,start_hours,end_hours,balance,no_discount,limit_start,perlimitdan,to86yk_tid,product_type";
            }else{
                $field = "id,aid,bid,cid,pic,name,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint,freighttype,freightdata,perlimit,gettj,gettjtip,gettjurl,scoredkmaxset,scoredkmaxval,status,start_time,end_time,start_hours,end_hours,balance,no_discount,limit_start,perlimitdan,product_type";
            }
            if(getcustom('plug_tengrui')) {
                $field .= ',house_status,group_status,group_ids,is_rzh,relation_type';
            }
            if(getcustom('product_glass')) {
                list($proid,$ggid,$num,$glass_record_id) = explode(',',$gwc);
            }
            if(getcustom('shop_yuding')) {
                list($proid,$ggid,$num,$glass_record_id) = explode(',',$gwc);
                $field .= ',stock,yuding_stock';
            }
            if(getcustom('product_moneypay')) {
                $field .= ',product_moneypay';
            }
            if(getcustom('product_bind_mendian')){
                $field .= ',bind_mendian_ids';
            }
		    if(getcustom('weight_template')){
                $field .= ',weightdata,weighttype';
            }
            if(getcustom('discount_code_zhongchuang')){
                $field .= ',price_discount_code_zc';
            }
            if(getcustom('product_wholesale')){
                $field .= ',jieti_discount_data,jieti_discount_type';
            }
			$product = Db::name('shop_product')->field($field)->where('aid',aid)->where('ischecked',1)->where('id',$proid)->find();

			if(!$product){
				Db::name('shop_cart')->where('aid',aid)->where('proid',$proid)->delete();
				return $this->json(['status'=>0,'msg'=>'产品不存在或已下架']);
			}
			//商品类型数组
			if(!in_array($product['product_type'],$protypes)){
				array_push($protypes,$product['product_type']);
			}
			//判断商家是否还存在
            if($product['bid']>0){
                $business = Db::name('business')->where('id',$product['bid'])->find();
                if(!$business){
                    return $this->json(['status'=>0,'msg'=>'产品['.$product['name'].']异常，请重新下单']);
                }
            }
			if(isset($product['product_moneypay'])){
			    if(!in_array($product['product_moneypay'],$productMoneyPay)){
                    $productMoneyPay[] = $product['product_moneypay'];
                }
            }
			if($product['status']==0){
				return $this->json(['status'=>0,'msg'=>'商品未上架']);
			}
			if($product['status']==2 && (strtotime($product['start_time']) > time() || strtotime($product['end_time']) < time())){
				return $this->json(['status'=>0,'msg'=>'商品未上架']);
			}
			if($product['status']==3){
				$start_time = strtotime(date('Y-m-d '.$product['start_hours']));
				$end_time = strtotime(date('Y-m-d '.$product['end_hours']));
				if(($start_time < $end_time && ($start_time > time() || $end_time < time())) || ($start_time >= $end_time && ($start_time > time() && $end_time < time()))){
					return $this->json(['status'=>0,'msg'=>'商品未上架']);
				}
			}
			if(getcustom('product_glass')){
			    if($product['product_type']==1){
			        $glassProductNum++;
                }
			    if($glass_record_id){
			        $glassrecord = Db::name('glass_record')->where('aid',aid)->where('mid',$this->mid)->where('id',$glass_record_id)->find();
			        if($glassrecord){
                        $product['has_glassrecord'] = 1;
                        $product['glassrecord'] = $glassrecord;
                    }
			    }
            }

			if($product['freighttype'] == 3 || $product['freighttype'] == 4) $autofahuo = $product['freighttype'];
			$guige = Db::name('shop_guige')->where('id',$ggid)->find();
			if(!$guige){
				Db::name('shop_cart')->where('aid',aid)->where('ggid',$ggid)->delete();
				return $this->json(['status'=>0,'msg'=>'产品该规格不存在或已下架']);
			}

            if(getcustom('shop_yuding')){
                if($product['stock'] <=0  && $product['yuding_stock'] > 0){
                    Db::name('shop_guige')->where('aid',aid)->where('id',$guige['id'])->update(['stock'=>Db::raw("stock+$num")]);
//                    Db::name('shop_product')->where('aid',aid)->where('id',$product['id'])->update(['stock'=>Db::raw("stock+$num")]);
                    $guige['stock']  = $num;
                }
            }

			if($guige['stock'] < $num){
				return $this->json(['status'=>0,'msg'=>'库存不足']);
			}
			$gettj = explode(',',$product['gettj']);
			if(!in_array('-1',$gettj) && !in_array($this->member['levelid'],$gettj) && (!in_array('0',$gettj) || $this->member['subscribe']!=1)){ //不是所有人
				if(!$product['gettjtip']) $product['gettjtip'] = '没有权限购买该商品';
				return $this->json(['status'=>0,'msg'=>$product['gettjtip'],'url'=>$product['gettjurl']]);
			}

            if($guige['limit_start'] > 0 && $num < $guige['limit_start']){
                return $this->json(['status'=>0,'msg'=>'['.$product['name'].']['.$guige['name'].'] '.$guige['limit_start'].'件起售']);
            }
			$limit_start_state = 1;
			if(getcustom('product_wholesale') && $product['product_type'] == 4){
				$jieti_discount_type = $product['jieti_discount_type'];
				if($jieti_discount_type == 0){
					$limit_start_state = 0;
				}

			}
			if($limit_start_state==1 && $product['limit_start'] > 0 && $num < $product['limit_start']){
				return $this->json(['status'=>0,'msg'=>'['.$product['name'].'] '.$product['limit_start'].'件起售']);
			}
			
            
            if($product['perlimitdan'] > 0 && $num > $product['perlimitdan']){
                return $this->json(['status'=>0,'msg'=>'['.$product['name'].'] 每单限购'.$product['perlimitdan'].'件']);
            }

			if($product['perlimit'] > 0){
				$buynum = $num + Db::name('shop_order_goods')->where('aid',aid)->where('mid',mid)->where('proid',$product['id'])->where('status','in','0,1,2,3')->sum('num');
				if($buynum > $product['perlimit']){
					return $this->json(['status'=>0,'msg'=>'['.$product['name'].'] 每人限购'.$product['perlimit'].'件']);
				}else{
                    if(isset($productLimit[$product['id']])){
                        $productLimit[$product['id']]['buyed'] += $buynum - $num;
                        $productLimit[$product['id']]['buy'] += $num;
                    }else{
                        $productLimit[$product['id']]['buyed'] = $buynum - $num;
                        $productLimit[$product['id']]['buy'] = $num;
                        $productLimit[$product['id']]['perlimit'] = $product['perlimit'];
                        $productLimit[$product['id']]['name'] = $product['name'];
                    }
                }
			}

            if(getcustom('shop_categroy_limit')){
                //判断分类是否限购
                if($product['cid']){
                    $category = Db::name('shop_category')->where('id','in',$product['cid'])->where('limit_num', '>',0)->find();
                    if($category) {
                        $product_cid_arr = explode(',',$product['cid']);
                        foreach ($product_cid_arr as $product_cid_arr_v){
                            if($cat_buynum[$product_cid_arr_v])
                                $cat_buynum[$product_cid_arr_v] += $num;
                            else
                                $cat_buynum[$product_cid_arr_v] = $num;
                        }

                        if($category['limit_day'] > 0){
                            $buynum = $cat_buynum[$category['id']] + Db::name('shop_order_goods')->where('aid',aid)->where('mid',mid)->whereFindInSet('cid',$category['id'])
                                    ->where('createtime', '>=', time()-$category['limit_day']*86400 )->where('status','in','0,1,2,3')->sum('num');
                            if($buynum > $category['limit_num']){
                                return $this->json(['status'=>0,'msg'=>'[分类:'.$category['name'].'] 每人每'.$category['limit_day'].'天限购'.$category['limit_num'].'件']);
                            }
                        }else {
                            $buynum = $cat_buynum[$category['id']] + Db::name('shop_order_goods')->where('aid',aid)->where('mid',mid)->whereFindInSet('cid',$product['cid'])
                                    ->where('status','in','0,1,2,3')->sum('num');
                            if($buynum > $category['limit_num']){
                                return $this->json(['status'=>0,'msg'=>'[分类:'.$category['name'].'] 每人限购'.$category['limit_num'].'件']);
                            }
                        }
                    }
                }
            }

            if(getcustom('plug_tengrui')) {
                //判断是否是否符合会员认证、会员关系、一户
                $tr_check = new \app\common\TengRuiCheck();
                $check_product = $tr_check->check_product($this->member,$product);
                if($check_product && $check_product['status'] == 0){
                    return $this->json(['status'=>$check_product['status'],'msg'=>$check_product['msg']]);
                }
                $tr_roomId = $check_product['tr_roomId'];
            }
			$guige = $this->formatguige($guige, $product['bid'],$product['lvprice']);
			if(getcustom('product_wholesale') && $product['product_type'] == 4){
				$jieti_num = $num;
				$jieti_discount_type = $product['jieti_discount_type'];
				if($jieti_discount_type == 0){
					foreach($prodata as $kn=>$v){
						list($proid_n,$ggid_n,$num_n) = explode(',',$v);
						if($proid_n ==$proid && $ggid_n !=$ggid){
							$jieti_num +=$num_n;
						}
					}
					if($product['product_type'] == 4 && $product['jieti_discount_type']==0){
						if($product['limit_start'] > $jieti_num){
							return $this->json(['status'=>0,'msg'=>'['.$product['name'].'] '.$product['limit_start'].'件起售']);
						}						
					}
				}				
				$guige = $this->formatguigewholesale($guige,$product,$jieti_num);
			}

			if($product['scoredkmaxset']==0){
				if($userinfo['scoredkmaxpercent'] == 0){
					$userinfo['scoremaxtype'] = 1;
					$scoredkmaxmoney += 0;
				}else{
					if($userinfo['scoredkmaxpercent'] > 0 && $userinfo['scoredkmaxpercent']<100){
						$scoredkmaxmoney += $userinfo['scoredkmaxpercent'] * 0.01 * $guige['sell_price'] * $num;
					}else{
						$scoredkmaxmoney += $guige['sell_price'] * $num;
					}
				}
			}elseif($product['scoredkmaxset']==1){
				$userinfo['scoremaxtype'] = 1;
				$scoredkmaxmoney += $product['scoredkmaxval'] * 0.01 * $guige['sell_price'] * $num;
			}elseif($product['scoredkmaxset']==2){
				$userinfo['scoremaxtype'] = 1;
				$scoredkmaxmoney += $product['scoredkmaxval'] * $num;
			}else{
				$userinfo['scoremaxtype'] = 1;
				$scoredkmaxmoney += 0;
			}
			if(getcustom('business_selfscore') && $business_selfscore == 1){
				if(!$scoredkdataArr[$product['bid']]){
					if($product['bid'] == 0){
						$scoredkdata = [];
						$scoredkdata['usescore'] = 0;
						$scoredkdata['scorebdkyf'] = $this->sysset['scorebdkyf'];
						$scoredkdata['score'] = $userinfo['score'];
						$scoredkdata['score2money'] = $userinfo['score2money'];
						$scoredkdata['scoredk_money'] = $userinfo['scoredk_money'];
						$scoredkdata['scoredkmaxpercent'] = $userinfo['scoredkmaxpercent'];
						$scoredkdata['scoremaxtype'] = $userinfo['scoremaxtype'];
						$scoredkdata['scoredkmaxmoney'] = $userinfo['scoredkmaxmoney'];
						$scoredkdataArr[$product['bid']] = $scoredkdata;
					}else{
						$business = Db::name('business')->where('id',$product['bid'])->find();
						$memberscore = Db::name('business_memberscore')->where('aid',aid)->where('bid',$product['bid'])->where('mid',mid)->value('score');
						$scoredkdata = [];
						$scoredkdata['usescore'] = 0;
						$scoredkdata['scorebdkyf'] = $business['scoreset']==0 ? $this->sysset['score2money'] : $business['scorebdkyf'];
						$scoredkdata['score'] = $memberscore ?? 0;
						$scoredkdata['score2money'] = $business['scoreset']==0 ? $this->sysset['score2money'] : $business['score2money'];
						$scoredkdata['scoredk_money'] = round($scoredkdata['score'] * $scoredkdata['score2money'],2);
						$scoredkdata['scoredkmaxpercent'] = $business['scoreset']==0 ? $this->sysset['scoredkmaxpercent'] : $business['scoredkmaxpercent'];
						$scoredkdata['scoremaxtype'] = 0;
						$scoredkdata['scoredkmaxmoney'] = 0;
						$scoredkdataArr[$product['bid']] = $scoredkdata;
					}
				}
				if($product['scoredkmaxset']==0){
					if($scoredkdataArr[$product['bid']]['scoredkmaxpercent'] == 0){
						$scoredkdataArr[$product['bid']]['scoremaxtype'] = 1;
						$scoredkdataArr[$product['bid']]['scoredkmaxmoney'] += 0;
					}else{
						if($scoredkdataArr[$product['bid']]['scoredkmaxpercent'] > 0 && $scoredkdataArr[$product['bid']]['scoredkmaxpercent']<100){
							$scoredkdataArr[$product['bid']]['scoredkmaxmoney'] += $scoredkdataArr[$product['bid']]['scoredkmaxpercent'] * 0.01 * $guige['sell_price'] * $num;
						}else{
							$scoredkdataArr[$product['bid']]['scoredkmaxmoney'] += $guige['sell_price'] * $num;
						}
					}
				}elseif($product['scoredkmaxset']==1){
					$scoredkdataArr[$product['bid']]['scoremaxtype'] = 1;
					$scoredkdataArr[$product['bid']]['scoredkmaxmoney'] += $product['scoredkmaxval'] * 0.01 * $guige['sell_price'] * $num;
				}elseif($product['scoredkmaxset']==2){
					$scoredkdataArr[$product['bid']]['scoremaxtype'] = 1;
					$scoredkdataArr[$product['bid']]['scoredkmaxmoney'] += $product['scoredkmaxval'] * $num;
				}else{
					$scoredkdataArr[$product['bid']]['scoremaxtype'] = 1;
					$scoredkdataArr[$product['bid']]['scoredkmaxmoney'] += 0;
				}
			}
			$product['weightkey'] = 0;
			$product['weightlist'] = [];
			if(getcustom('weight_template')){
				$weightlist = [];
				if($product['weighttype']>0){
					if($product['weighttype']==1){
						$weightlist = Db::name('shop_weight_template')->where('aid',aid)->where('status',1)->select()->toArray();
					}elseif($product['weighttype']==2){
						$weightdata = explode(',',$product['weightdata']);
						$weightlist = Db::name('shop_weight_template')->where('aid',aid)->where('id','in',$weightdata)->where('status',1)->select()->toArray();
					}
				}
				if($weightlist){
					$weight = $guige['weight'] * $num;
					$rs = \app\model\Weight::formatWeightList($weightlist,$weight);		
					//var_dump($rs);
					$product['weightlist'] = $rs['weight'];
				}
			}

			if(!$allbuydata[$product['bid']]) $allbuydata[$product['bid']] = [];
			if(!$allbuydata[$product['bid']]['prodata']) $allbuydata[$product['bid']]['prodata'] = [];
			$allbuydata[$product['bid']]['prodata'][] = ['product'=>$product,'guige'=>$guige,'num'=>$num];
			if($product['to86yk_tid']){
				$extendInput = [['key'=>'input','val1'=>'充值账号','val2'=>'请输入充值账号','val3'=>1],['key'=>'input','val1'=>'确认账号','val2'=>'请再次输入充值账号','val3'=>1]];
			}
		}
        //限购判断
        if($productLimit){
            foreach ($productLimit as $pitem){
                if($pitem['buy']+$pitem['buyed'] > $pitem['perlimit']){
                    return $this->json(['status'=>0,'msg'=>'['.$pitem['name'].'] 每人限购'.$pitem['perlimit'].'件']);
                }
            }
        }
		//支付方式不同，不允许同时结算
        if(getcustom('product_show_moneyprice')){
            if(count($productMoneyPay)>1){
                return $this->json(['status'=>0,'msg'=>'支付方式不同，请分开结算']);
            }
        }
		if($autofahuo>0 && count($prodata) > 1){
			return $this->json(['status'=>0,'msg'=>'虚拟商品请单独购买']);
		}
		if(getcustom('product_handwork')){
            //手工活类单独下单
            if(in_array(3,$protypes)){
            	$ptnum = count($protypes);
            	if($ptnum>1){
            		return $this->json(['status'=>0,'msg'=>'手工活类商品请单独购买']);
            	}
            }
        }

        if(getcustom('money_dec')){
            //总平台余额抵扣
            $moneydec = false;
        }
		$userinfo['scoredkmaxmoney'] = round($scoredkmaxmoney,2);
		$havetongcheng = 0;
		if(getcustom('shop_buy_worknum')){
        	$worknum_status=false;//是否显示工号
        }
		foreach($allbuydata as $bid=>$buydata){

            if(getcustom('money_dec')){
                $allbuydata[$bid]['money_dec_rate'] = 0;
                if(empty($bid)){
                    $money_dec_rate = 0;//抵扣比例
                    if($adminset['money_dec'] && $adminset['money_dec_rate']>0){
                        $moneydec = true;
                        $allbuydata[$bid]['money_dec_rate'] = $adminset['money_dec_rate'];
                    }
                }else{
                    //查询商户余额抵扣比例
                    $business = Db::name('business')->where(['aid'=>aid,'id'=>$bid])->field('money_dec,money_dec_rate')->find();
                    if($business && $business['money_dec'] && $business['money_dec_rate']>0){
                        $moneydec = true;
                        $allbuydata[$bid]['money_dec_rate'] = $business['money_dec_rate'];
                    }
                }
            }

			if(getcustom('guige_split')){
				$rs = \app\model\ShopProduct::checkstock($buydata['prodata']);
				if($rs['status'] == 0) return $this->json($rs);
			}
			if($autofahuo>0){
				$freightList = [['id'=>0,'name'=>($autofahuo==3?'自动发货':'在线卡密'),'pstype'=>$autofahuo]];
			}else{
				$freightList = \app\model\Freight::getList([['status','=',1],['aid','=',aid],['bid','=',$bid]]);
				$fids = [];
				foreach($freightList as $v){
					$fids[] = $v['id'];
				}
				foreach($buydata['prodata'] as $prodata){
					if($prodata['product']['freighttype']==0){
						$fids = array_intersect($fids,explode(',',$prodata['product']['freightdata']));
					}else{
						$thisfreightList = \app\model\Freight::getList([['status','=',1],['aid','=',aid],['bid','=',$bid]]);
						$thisfids = [];
						foreach($thisfreightList as $v){
							$thisfids[] = $v['id'];
						}
						$fids = array_intersect($fids,$thisfids);
					}
				}
				if(!$fids){
					if(count($buydata['prodata'])>1){
						return $this->json(['status'=>0,'msg'=>'所选择商品配送方式不同，请分别下单']);
					}else{
						return $this->json(['status'=>0,'msg'=>'获取配送方式失败']);
					}
				}
				$freightList = \app\model\Freight::getList([['status','=',1],['aid','=',aid],['bid','=',$bid],['id','in',$fids]]);

				foreach($freightList as $k=>$v){
					//var_dump($freightList);
					if($v['pstype']==2){ //同城配送
						$havetongcheng = 1;
					}
					if(getcustom('hmy_yuyue') && $v['pstype']==12){
						//红蚂蚁定制
						$havetongcheng = 1;
					}
					if(getcustom('shop_buy_worknum')){
						if($k == 0 && $v['worknum_status']){
							$worknum_status=true;//是否显示工号
						}
			        }
				}
			}
			$allbuydata[$bid]['freightList'] = $freightList;
		}
		if($havetongcheng){
			$address = Db::name('member_address')->where('aid',aid)->where('mid',mid)->where('latitude','>',0)->order('isdefault desc,id desc')->find();
		}else{
			$address = Db::name('member_address')->where('aid',aid)->where('mid',mid)->order('isdefault desc,id desc')->find();
		}
		if(!$address) $address = [];
		$needLocation = 0;
		$allproduct_price = 0;
		foreach($allbuydata as $bid=>$buydata){
			if($bid!=0){
				$business = Db::name('business')->where('id',$bid)->field('id,aid,cid,name,logo,tel,address,sales,longitude,latitude,start_hours,end_hours,start_hours2,end_hours2,start_hours3,end_hours3,end_buy_status,invoice,invoice_type,is_open')->find();
                if($business['is_open']==0) return $this->json(['status'=>-4,'msg'=>$business['name'].'未营业']);
				$is_open = 0;
				if($is_open==0){
					if($business['start_hours'] != $business['end_hours']){
						$start_time = strtotime(date('Y-m-d '.$business['start_hours']));
						$end_time = strtotime(date('Y-m-d '.$business['end_hours']));
						if(($start_time < $end_time && ($start_time > time() || $end_time < time())) || ($start_time > $end_time && ($start_time > time() && $end_time < time()))){
							//return $this->json(['status'=>-4,'msg'=>'商家不在营业时间']);
						}else{
							$is_open = 1;
						}
					}else{
						$is_open = 1;
					}
				}
				if($is_open==0){
					$start_time = strtotime(date('Y-m-d '.$business['start_hours2']));
					$end_time = strtotime(date('Y-m-d '.$business['end_hours2']));
					if($start_time == $end_time || ($start_time < $end_time && ($start_time > time() || $end_time < time())) || ($start_time > $end_time && ($start_time > time() && $end_time < time()))){
						//return $this->json(['status'=>-4,'msg'=>'商家不在营业时间']);
					}else{
						$is_open = 1;
					}
				}
				if($is_open==0){
					$start_time = strtotime(date('Y-m-d '.$business['start_hours3']));
					$end_time = strtotime(date('Y-m-d '.$business['end_hours3']));
					if($start_time == $end_time || ($start_time < $end_time && ($start_time > time() || $end_time < time())) || ($start_time > $end_time && ($start_time > time() && $end_time < time()))){
						//return $this->json(['status'=>-4,'msg'=>'商家不在营业时间']);
					}else{
						$is_open = 1;
					}
				}
				if($is_open == 0 && $business['end_buy_status'] == 0){
                    $open_time = $business['start_hours'].'-'.$business['end_hours'];
                    if($business['start_hours2'] != $business['end_hours2']){
                        $open_time .= ' '.$business['start_hours2'].'-'.$business['end_hours2'];
                    }
                    if($business['start_hours3'] != $business['end_hours3']){
                        $open_time .= ' '.$business['start_hours3'].'-'.$business['end_hours3'];
                    }
                    return $this->json(['status'=>-4,'msg'=>'商家已打烊，营业时间为:'.$open_time]);
				}
			}else{
				$business = Db::name('admin_set')->where('aid',aid)->field('id,name,logo,desc,tel,invoice,invoice_type,invoice_rate')->find();
			}
            $business['invoice_type'] = $business['invoice'] ? explode(',', $business['invoice_type']) : [];
			$product_priceArr = [];
			$product_price = 0;
			$needzkproduct_price = 0;
			$totalweight = 0;
			$totalnum = 0;
			$prodataArr = [];
			$proids = [];
			$cids = [];
            //符合满减的商品价格
            $mj_price = 0;
            $bindMendianIds = [];
			foreach($buydata['prodata'] as &$prodata){
				$product_priceArr[] = $prodata['guige']['sell_price'] * $prodata['num'];
				$product_price += $prodata['guige']['sell_price'] * $prodata['num'];
				if($prodata['product']['balance']){
					$product_price = $product_price * (1-$prodata['product']['balance']*0.01);
				}
				if($prodata['product']['lvprice']==0 && $prodata['product']['no_discount'] == 0){ //未开启会员价
					$needzkproduct_price += $prodata['guige']['sell_price'] * $prodata['num'];
				}
                if(getcustom('product_bind_mendian')){
                    if($prodata['product']['bind_mendian_ids'] && !in_array('-1',explode(',',$prodata['product']['bind_mendian_ids']))){
                        $bindMendianIds = array_unique(array_merge($bindMendianIds,explode(',',$prodata['product']['bind_mendian_ids'])));
                    }
                }
				$totalweight += $prodata['guige']['weight'] * $prodata['num'];
				$totalnum += $prodata['num'];
				$_prostr = $prodata['product']['id'].','.$prodata['guige']['id'].','.$prodata['num'];
				if(isset($prodata['product']['glassrecord']) && $prodata['product']['glassrecord']){
                    $_prostr .=','.$prodata['product']['glassrecord']['id'];
                }
				$prodataArr[] = $_prostr;
				$proids[] = $prodata['product']['id'];
				$cids = array_merge(explode(',',$prodata['product']['cid']),$cids);
                //如果满减设置存在，且数据存在
                if($mjset && $mjdata){
                    //指定分类
                    if($mjset['fwtype']==1){
                        //指定分类数组
                        $cat_arr     = explode(",",$mjset['categoryids']);
                        //商品分类
                        $pro_cat_arr = explode(",",$prodata['product']['cid']);
                        //交集
                        $j_arr = array_intersect($cat_arr,$pro_cat_arr);
                        if($j_arr){
                            $mj_price += $prodata['guige']['sell_price'] * $prodata['num'];
                            if($prodata['product']['balance']){
                                $mj_price = $mj_price * (1-$prodata['product']['balance']*0.01);
                            }
                        }
                    //指定商品
                    }else if($mjset['fwtype']==2){
                        $pro_arr = explode(",",$mjset['productids']);
                        //商品在指定商品内
                        if (in_array($prodata['product']['id'], $pro_arr)){
                            $mj_price += $prodata['guige']['sell_price'] * $prodata['num'];
                            if($prodata['product']['balance']){
                                $mj_price = $mj_price * (1-$prodata['product']['balance']*0.01);
                            }
                        }
                    }else{
                        $mj_price += $prodata['guige']['sell_price'] * $prodata['num'];
                        if($prodata['product']['balance']){
                            $mj_price = $mj_price * (1-$prodata['product']['balance']*0.01);
                        }
                    }
                }
			}
			$prodatastr = implode('-',$prodataArr);
			
            $rs = \app\model\Freight::formatFreightList($buydata['freightList'],$address,$product_price,$totalnum,$totalweight,$bindMendianIds,$mendian_id);

			$freightList = $rs['freightList'];
			$freightArr = $rs['freightArr'];
			if($rs['needLocation']==1) $needLocation = 1;
			$leveldk_money = 0;
			if($userlevel && $userlevel['discount']>0 && $userlevel['discount']<10){
				$leveldk_money = $needzkproduct_price * (1 - $userlevel['discount'] * 0.1);
			}
			$leveldk_money = round($leveldk_money,2);
			$price = $product_price - $leveldk_money;
            $mj_price = $mj_price-$leveldk_money;

			$manjian_money = 0;
			$moneyduan = 0;
			if($mjdata && $mj_price>0){
                //如果是累计消费额满减
                if($mjset['total_status']==1){
                    //指定分类
                    if($mjset['fwtype']==1){
                        //查询他分类消费累计
                        $sum_money  = Db::name('shop_order_goods')
                            ->alias('sog')
                            ->join('shop_order so','so.id   = sog.orderid')
                            ->join('shop_product sp','sp.id = sog.proid')
                            ->where('sog.mid',mid)
                            ->where('so.status',3)
                            ->where('sp.cid','in',$mjset['categoryids'])
                            ->sum('sog.totalprice');

                        //分类退款累计
                        $refund_money = Db::name('shop_refund_order_goods')
                            ->alias('srog')
                            ->join('shop_order so','so.id   = srog.orderid')
                            ->join('shop_product sp','sp.id = srog.proid')
                            ->where('srog.mid',mid)
                            ->where('so.status',3)
                            ->where('so.refund_status',2)
                            ->where('sp.cid','in',$mjset['categoryids'])
                            ->sum('srog.refund_money');

                    //指定商品
                    }else if($mjset['fwtype']==2){
                        //查询他商品消费累计
                        $sum_money  = Db::name('shop_order_goods')
                            ->alias('sog')
                            ->join('shop_order so','so.id   = sog.orderid')
                            ->join('shop_product sp','sp.id = sog.proid')
                            ->where('sog.mid',mid)
                            ->where('so.status',3)
                            ->where('sog.proid','in',$mjset['productids'])
                            ->sum('sog.totalprice');

                        //商品退款累计
                        $refund_money = Db::name('shop_refund_order_goods')
                            ->alias('srog')
                            ->join('shop_order so','so.id   = srog.orderid')
                            ->join('shop_product sp','sp.id = srog.proid')
                            ->where('srog.mid',mid)
                            ->where('so.status',3)
                            ->where('so.refund_status',2)
                            ->where('srog.proid','in',$mjset['productids'])
                            ->sum('srog.refund_money');
                    //所有
                    }else{
                        //查询他消费累计
                        $sum_money    = Db::name('shop_order')->where('mid',mid)->where('status',3)->sum('totalprice');
                        //退款累计
                        $refund_money = Db::name('shop_order')->where('mid',mid)->where('status',3)->where('refund_status',2)->sum('refund_money');
                    }

                    //实际累计
                    $sj_money = $sum_money-$refund_money;
                    $sj_money = round($sj_money,2);
                    $all_price = $sj_money+$mj_price;
                    foreach($mjdata as $give){
                        if(($all_price - $leveldk_money)*1 >= $give['money']*1 && $give['money']*1 > $moneyduan){
                            $moneyduan = $give['money']*1;
                            $manjian_money = $give['jian']*1;
                        }
                    }
                }else{
                    foreach($mjdata as $give){
                        if(($mj_price - $leveldk_money)*1 >= $give['money']*1 && $give['money']*1 > $moneyduan){
                            $moneyduan = $give['money']*1;
                            $manjian_money = $give['jian']*1;
                        }
                    }
                }

			}
			if($manjian_money > 0){
				$allbuydata[$bid]['manjian_money'] = round($manjian_money,2);
			}else{
				$allbuydata[$bid]['manjian_money'] = 0;
			}

			if($bid > 0){
				$business = Db::name('business')->where('aid',aid)->where('id', $bid)->find();
				$bcids = $business['cid'] ? explode(',',$business['cid']) : [];
			}else{
				$bcids = [];
			}
			if($bcids){
				$whereCid = [];
				foreach($bcids as $bcid){
					$whereCid[] = "find_in_set({$bcid},canused_bcids)";
				}
				$whereCids = implode(' or ',$whereCid);
			}else{
				$whereCids = '0=1';
			}
			
			$newcouponlist = [];
			$couponList = Db::name('coupon_record')->where('aid',aid)->where('mid',mid)->where('type','in','1,4,10,11')->where('status',0)
				->whereRaw("bid=-1 or bid=".$bid." or (bid=0 and (canused_bids='all' or find_in_set(".$bid.",canused_bids) or ($whereCids)))")->where('minprice','<=',$price - $manjian_money)->where('starttime','<=',time())->where('endtime','>',time())
				->order('id desc')->select()->toArray();
			if(!$couponList) $couponList = [];
			foreach($couponList as $k=>$v){
				//$couponList[$k]['starttime'] = date('m-d H:i',$v['starttime']);
				//$couponList[$k]['endtime'] = date('m-d H:i',$v['endtime']);
				$couponinfo = Db::name('coupon')->where('aid',aid)->where('id',$v['couponid'])->find();

                if($v['from_mid']) {
                    //已经被领取，不可再次赠送，属于仅自用
                    $couponinfo['isgive'] = 0;
                }else{
                    //仅转赠
                    if($couponinfo['isgive']==2){
                        continue;
                    }
                }

                //0全场通用,1指定类目,2指定商品
                if(!in_array($couponinfo['fwtype'],[0,1,2])){
                    continue;
                }
                //适用场景
                if(!in_array($couponinfo['fwscene'],[0])){
                    continue;
                }
				$usetj = explode(',',$couponinfo['usetj']);
				if(!in_array('-1',$usetj) && !in_array($this->member['levelid'],$usetj) && (!in_array('0',$usetj) || $this->member['subscribe']!=1)){
					continue;
				}

				$v['thistotalprice'] = $price - $manjian_money;
                $v['couponmoney'] = $v['money'];
				if($couponinfo['fwtype']==2){//指定商品可用
					$productids = explode(',',$couponinfo['productids']);
					if(!array_intersect($proids,$productids)){
						continue;
					}
                    $v['productids'] = $productids;
                    $thistotalprice = 0;
					foreach($buydata['prodata'] as $k2=>$v2){
						$product = $v2['product'];
						if(in_array($product['id'],$productids)){
							$thistotalprice += $v2['guige']['sell_price'] * $v2['num'];
                            $v['sell_price'] =  $v2['guige']['sell_price'];
						}
					}
					if($thistotalprice < $v['minprice']){
						continue;
					}
					$v['thistotalprice'] = $thistotalprice;
                    $v['couponmoney'] = min($thistotalprice,$v['money']);//可抵扣金额
				}
				if($couponinfo['fwtype']==1){//指定类目可用
					if(!$couponinfo['categoryids']) continue;
					$categoryids = explode(',',$couponinfo['categoryids']);
					$clist = Db::name('shop_category')->where('pid','in',$categoryids)->select()->toArray();
					foreach($clist as $kc=>$vc){
						$categoryids[] = $vc['id'];
						$cate2 = Db::name('shop_category')->where('pid',$vc['id'])->find();
						if($cate2) $categoryids[] = $cate2['id'];
					}
					if(!array_intersect($cids,$categoryids)){
						continue;
					}
					$thistotalprice = 0;
					foreach($buydata['prodata'] as $k2=>$v2){
						$product = $v2['product'];
						if(array_intersect(explode(',',$product['cid']),$categoryids)){
							$thistotalprice += $v2['guige']['sell_price'] * $v2['num'];
                            $v['sell_price'] =  $v2['guige']['sell_price'];
						}
					}
					if($thistotalprice < $v['minprice']){
						continue;
					}
					$v['thistotalprice'] = $thistotalprice;
                    $v['couponmoney'] = min($thistotalprice,$v['money']);//可抵扣金额
				}
				if($v['bid'] > 0){
					$binfo = Db::name('business')->where('aid',aid)->where('id',$v['bid'])->find();
					$v['bname'] = $binfo['name'];
				}
				if(getcustom('coupon_not_used_discount')){
                    $v['not_used_discount'] =  $couponinfo['not_used_discount'];
                }
               
				$newcouponlist[] = $v;
			}
			$couponList = $newcouponlist;

			//促销活动
			$cuxiaolist = Db::name('cuxiao')->where('aid',aid)->where('bid',$bid)->where("(type in (1,2,3,4) and minprice<=".($price - $manjian_money).") or ((type=5 or type=6) and minnum<=".$totalnum.") ")->where('starttime','<',time())->where('endtime','>',time())->order('sort desc')->select()->toArray();
			$newcxlist = [];
			foreach($cuxiaolist as $k=>$v){
				$gettj = explode(',',$v['gettj']);
				if(!in_array('-1',$gettj) && !in_array($this->member['levelid'],$gettj)){ //不是所有人
					continue;
				}
				if($v['fwtype']==2){//指定商品可用
					$productids = explode(',',$v['productids']);
					if(!array_intersect($proids,$productids)){
						continue;
					}
					if($v['type']==1 || $v['type']==2 || $v['type']==3 || $v['type']==4){//指定商品是否达到金额要求
						$thistotalprice = 0;
						foreach($buydata['prodata'] as $k2=>$v2){
							$product = $v2['product'];
							if(in_array($product['id'],$productids)){
								$thistotalprice += $v2['guige']['sell_price'] * $v2['num'];
							}
						}
						if($thistotalprice < $v['minprice']){
							continue;
						}
					}
					if($v['type']==6 || $v['type']==5){//指定商品是否达到件数要求
						$thistotalnum = 0;
						foreach($buydata['prodata'] as $k2=>$v2){
							$product = $v2['product'];
							if(in_array($product['id'],$productids)){
								$thistotalnum += $v2['num'];
							}
						}
						if($thistotalnum < $v['minnum']){
							continue;
						}
					}
				}
				if($v['fwtype']==1){//指定类目可用
					if(!$v['categoryids']) continue;
					$categoryids = explode(',',$v['categoryids']);
					$clist = Db::name('shop_category')->where('pid','in',$categoryids)->select()->toArray();
					foreach($clist as $kc=>$vc){
						$categoryids[] = $vc['id'];
						$cate2 = Db::name('shop_category')->where('pid',$vc['id'])->find();
						if($cate2) $categoryids[] = $cate2['id'];
					}
					if(!array_intersect($cids,$categoryids)){
						continue;
					}
					if($v['type']==1 || $v['type']==2 || $v['type']==3 || $v['type']==4){//指定商品是否达到金额要求
						$thistotalprice = 0;
						foreach($buydata['prodata'] as $k2=>$v2){
							$product = $v2['product'];
							if(array_intersect(explode(',',$product['cid']),$categoryids)){
								$thistotalprice += $v2['guige']['sell_price'] * $v2['num'];
							}
						}
						if($thistotalprice < $v['minprice']){
							continue;
						}
					}
					if($v['type']==6 || $v['type']==5){//指定类目内商品是否达到件数要求
						$thistotalnum = 0;
						foreach($buydata['prodata'] as $k2=>$v2){
							$product = $v2['product'];
							if(array_intersect(explode(',',$product['cid']),$categoryids)){
								$thistotalnum += $v2['num'];
							}
						}
						if($thistotalnum < $v['minnum']){
							continue;
						}
					}
				}
				if($v['type']==4 || $v['type']==5){
					if($v['fwtype']==2) {
					    //商品
                        $cuxiaomoney = 0;
                        $prozkArr = array_combine(explode(',', $v['productids']), explode(',', $v['prozk']));
                        $pronumArr = array_combine(explode(',', $v['productids']), explode(',', $v['pronum']));
                        foreach ($buydata['prodata'] as $k2 => $v2) {
                            $product = $v2['product'];
                            if ($prozkArr[$product['id']]) {
                                $prozk = $prozkArr[$product['id']];
                            } elseif (isset($prozkArr[$product['id']])) {
                                $prozk = $v['zhekou'];
                            } else {
                                $prozk = 10;
                            }
                            if ($v['type'] == 5 && $pronumArr[$product['id']] && intval($pronumArr[$product['id']]) > $v2['num']) {
                                $prozk = 10;
                            }
                            $cuxiaomoney += $product_priceArr[$k2] * (1 - $prozk * 0.1);
                        }
                    }elseif($v['fwtype']==1) {
					    //分类
					    $categoryPrice = 0;
                        foreach ($buydata['prodata'] as $k2 => $v2) {
                            $product = $v2['product'];
                            $cids2 = explode(',', $product['cid']);
                            if(array_intersect($cids2, $categoryids)) {
                                $categoryPrice += $v2['guige']['sell_price'] * $v2['num'];
                            }
                        }
                        $cuxiaomoney = $categoryPrice * (1 - $v['zhekou'] * 0.1);
					}else{
					    //全部
						$cuxiaomoney = $price * (1 - $v['zhekou'] * 0.1);
					}
					$v['cuxiaomoney'] = round($cuxiaomoney,2);
				}
                if(getcustom('plug_tengrui')) {
                    $tr_check = new \app\common\TengRuiCheck();
                    //判断是否是否符合会员认证、会员关系、一户
                    $check_cuxiao = $tr_check->check_cuxiao($this->member,$v);
                    if($check_cuxiao && $check_cuxiao['status'] == 0){
                        continue;
                    }
                }
				$newcxlist[] = $v;
			}
			
			if($extendInput){
				foreach($freightList as $fk=>$fv){
					$freightList[$fk]['formdata'] = array_merge($extendInput,$fv['formdata']);
				}
			}
            $invoice_price = 0;
			if($business['invoice'] && $business['invoice_rate'] > 0 && getcustom('invoice_rate')){
                $invoice_price = ($product_price + $freightList[0]['freight_price']) * $business['invoice_rate'] / 100;
            }
			$allbuydata[$bid]['bid'] = $bid;
			$allbuydata[$bid]['business'] = $business;
			$allbuydata[$bid]['prodatastr'] = $prodatastr;
			$allbuydata[$bid]['couponList'] = $couponList;
			$allbuydata[$bid]['couponCount'] = count($couponList);
			$allbuydata[$bid]['freightList'] = $freightList;
			$allbuydata[$bid]['freightArr'] = $freightArr;
			$allbuydata[$bid]['product_price'] = round($product_price,2);
            $allbuydata[$bid]['invoice_price'] = round($invoice_price,2);
			$allbuydata[$bid]['leveldk_money'] = $leveldk_money;
			$allbuydata[$bid]['coupon_money'] = 0;
			$allbuydata[$bid]['coupontype'] = 1;
			$allbuydata[$bid]['couponrid'] = 0;
			$allbuydata[$bid]['couponrids'] = [];
			$allbuydata[$bid]['coupons'] = [];
			$allbuydata[$bid]['freightkey'] = 0;
			$allbuydata[$bid]['pstimetext'] = '';
			$allbuydata[$bid]['freight_time'] = '';
			$allbuydata[$bid]['storeid'] = 0;
			$allbuydata[$bid]['storename'] = '';
			$allbuydata[$bid]['cuxiaolist'] = $newcxlist;
			$allbuydata[$bid]['cuxiaoCount'] = count($newcxlist);
			$allbuydata[$bid]['cuxiao_money'] = 0;
			$allbuydata[$bid]['cuxiaotype'] = 0;
			$allbuydata[$bid]['cuxiaoid'] = 0;
			$allbuydata[$bid]['invoice_money'] = 0;
			$allbuydata[$bid]['editorFormdata'] = [];

			$allbuydata[$bid]['coupon_peruselimit'] = 1;
			if(getcustom('coupon_peruselimit')){
				$shopset = Db::name('shop_sysset')->where('aid',aid)->find();
				$allbuydata[$bid]['coupon_peruselimit'] = $shopset['coupon_peruselimit'];
			}
            if(getcustom('coupon_xianxia_buy')){
                $allbuydata[$bid]['coupon_peruselimit'] =100;
            }
			$allproduct_price += $product_price;
		}
           
		if(getcustom('plug_xiongmao')) {
            $admin = Db::name('admin')->where('id',aid)->find();
            if(in_array('order_change_price', explode(',',$admin['remark']))) {
                $order_change_price = true;
            }
        }
        if(getcustom('shop_yuding')){
            //再减去规格库存
            if($product['stock'] <=0  && $product['yuding_stock'] > 0){
                Db::name('shop_guige')->where('aid',aid)->where('id',$guige['id'])->update(['stock'=>Db::raw("stock-$num")]);
            }
        }
        $custom = [];
		$rdata = [];
		$rdata['status'] = 1;
		$rdata['hasglassproduct'] = $glassProductNum>0?1:0;
		$rdata['havetongcheng'] = $havetongcheng;
		$rdata['address'] = $address;
		$rdata['linkman'] = $address ? $address['name'] : strval($userinfo['realname']);
		$rdata['tel'] = $address ? $address['tel'] : strval($userinfo['tel']);
		if(!$rdata['linkman']){
			$lastorder = Db::name('shop_order')->where('aid',aid)->where('mid',mid)->where('linkman','<>','')->find();
			if($lastorder){
				$rdata['linkman'] = $lastorder['linkman'];
				$rdata['tel'] = $lastorder['tel'];
			}
		}
		$rdata['userinfo'] = $userinfo;
		$rdata['allbuydata'] = $allbuydata;
		$rdata['bid'] = $bid;
		$rdata['business_payconfirm'] = 0;
		if(getcustom('business_payconfirm') && $bid > 0 && count($allbuydata) == 1){
			$rdata['business_payconfirm'] = 1;
		}

		$rdata['needLocation'] = $needLocation;
		$rdata['scorebdkyf'] = Db::name('admin_set')->where('aid',aid)->value('scorebdkyf');//积分抵扣运费 1不抵扣，0抵扣
		$rdata['buy_selectmember'] = false;
		if(getcustom('buy_selectmember')){
			$canselect = Db::name('member_level')->where('aid',aid)->where('can_buyselect',1)->find();
			if($canselect) $rdata['buy_selectmember'] = true;
		}
        $rdata['multi_promotion'] = $multi_promotion;
        $rdata['order_change_price'] = $order_change_price;
        $rdata['pstype3needAddress'] = false;
        if(getcustom('plug_zhiming')) {
            $rdata['pstype3needAddress'] = true;
        }

		$rdata['price_dollar'] = false;
		if(getcustom('price_dollar')){
			$usdrate = Db::name('shop_sysset')->where('aid',aid)->value('usdrate');
			if($usdrate>0){
				$rdata['usdrate'] =$usdrate;
			}
			$rdata['price_dollar'] = true;
		}
		$rdata['business_selfscore'] = $business_selfscore;
		$rdata['scoredkdataArr'] = $scoredkdataArr;

        if(getcustom('money_dec')){
            $rdata['moneydec'] = $moneydec;
        }
        if(getcustom('shop_buy_worknum')){
        	$rdata['worknum_status'] = $worknum_status?true:false;//是否显示工号
        	$rdata['worknumtip']     = '请输入您的工号(必须为10位数字)';//
        	
        }
        if(getcustom('shop_mendiantype')){
            //商城购买页门店搜索方式
        	$rdata['mendiantype'] = 1;
        }
        if(getcustom('discount_code_zhongchuang')){
            //优惠代码
            $custom[] = 'discount_code_zc';
        }
        $rdata['custom'] = $custom;
        //如果开启了优惠不同享 折扣金额为0
        $rdata['coupon_not_used_discount'] = 0;
        if(getcustom('coupon_not_used_discount') ){
            $coupon_not_used_discount = Db::name('shop_sysset')->where('aid',aid)->value('coupon_not_used_discount');
            $rdata['coupon_not_used_discount'] = $coupon_not_used_discount;
        }
        if(getcustom('product_handwork')){
        	$rdata['ishand']    = 0;
        	$rdata['hwcontent'] = '';
        	if($protypes && $protypes[0] == 3){
        		$rdata['ishand'] = 1;
        		$hwset = Db::name('shop_sysset')->where('aid',aid)->field('hwname,hwcontent')->find();
        		$rdata['hwset'] = $hwset?$hwset:'';
        	}
        }
		return $this->json($rdata);
	}
	public function createOrder(){
		$this->checklogin();
		$sysset = Db::name('admin_set')->where('aid',aid)->find();
		$post = input('post.');
        $score_weishu = $this->score_weishu;
        if(getcustom('discount_code_zhongchuang') && $post['discount_code_zc']){
            $rs = $this->checkDiscountCodeZc($post['discount_code_zc']);
            $rs = $rs->getData();
            if($rs['status'] != 1) {$post['discount_code_zc'] = '';}
        }
		//收货地址
		if($post['addressid']=='' || $post['addressid']==0){
			$address = ['id'=>0,'name'=>$post['linkman'],'tel'=>$post['tel'],'area'=>'','address'=>''];
		}else{
			$address = Db::name('member_address')->where('id',$post['addressid'])->where('aid',aid)->where('mid',mid)->find();
			if(!$address) return $this->json(['status'=>0,'msg'=>'所选收货地址不存在']);
		}
		$userlevel = Db::name('member_level')->where('aid',aid)->where('id',$this->member['levelid'])->find();
		$shopset = Db::name('shop_sysset')->where('aid',aid)->find();

		$buydata = $post['buydata'];
		$couponridArr = [];
		foreach($buydata as $data){ //判断有没有重复选择的优惠券
			if(!$data['couponrid']) continue;
			$couponrids = explode(',',$data['couponrid']);
			if(getcustom('coupon_xianxia_buy')){
                $shopset['coupon_peruselimit'] =100;
            }
			if($shopset['coupon_peruselimit'] < count($couponrids)){
				return $this->json(['status'=>0,'msg'=>'最多可用'.$shopset['coupon_peruselimit'].'张'.t('优惠券')]);
			}
			foreach($couponrids as $couponrid){
				if(in_array($couponrid,$couponridArr)){
					return $this->json(['status'=>0,'msg'=>t('优惠券').'不可重复使用']);
				}
				$couponridArr[] = $couponrid;
			}
		}

		$ordernum = \app\common\Common::generateOrderNo(aid);
		$scoredkmaxmoney = 0;
		$scoremaxtype = 0; //0按系统设置 1商品独立设置
		$i = 0;
		$alltotalprice = 0;
		$alltotalscore = 0;
        $cat_buynum=[];

        //满减活动
        $mjset = Db::name('manjian_set')->where('aid',aid)->find();
        if($mjset && $mjset['status']==1){
            $mjdata = json_decode($mjset['mjdata'],true);
            //指定分类
            if($mjset['fwtype']==1){
                //指定分类或商品分类不存在
                if(empty($mjset['categoryids'])){
                    $mjdata = array();
                }
            //指定商品
            }else if($mjset['fwtype']==2){
                if(empty($mjset['productids'])){
                    $mjdata = array();
                }
            }
            if(getcustom('plug_tengrui')) {
                if($mjdata){
                    $tr_check = new \app\common\TengRuiCheck();
                    //判断是否是否符合会员认证、会员关系、一户、用户组，不符合则直接去掉
                    $check_manjian = $tr_check->check_manjian($this->member,$mjset);
                    if($check_manjian && $check_manjian['status'] == 0){
                        $mjdata = array();
                    }
                    $manjian_tr_roomId = $check_manjian['tr_roomId'];
                }
            }
        }else{
            $mjdata = array();
        }

		$business_selfscore = 0;
		if(getcustom('business_selfscore')){
			$bset = Db::name('business_sysset')->where('aid',aid)->find();
			if($bset['business_selfscore'] == 1 && $bset['business_selfscore2'] == 1){
				$business_selfscore = 1;
			}
		}
		foreach($buydata as $data){
            $productTypes = [];
            $productType = 0;

            $protypes=[];//存在的商品类型
			$i++;
			$bid = $data['bid'];
			if($data['prodata']){
				$prodata = explode('-',$data['prodata']);
			}else{
				return $this->json(['status'=>0,'msg'=>'产品数据错误']);
			}
			$product_priceArr = [];
			$product_price = 0;
			$balance_price = 0;
			$needzkproduct_price = 0;
			$givescore = 0; //奖励积分 确认收货后赠送
			$givescore2 = 0; //奖励积分2 付款后赠送
			$totalweight = 0;//重量
			$totalnum = 0;
			$prolist = [];
			$proids = [];
			$cids = [];
			$invoice = [];
            $givetongzheng = 0; //奖励通证 确认收货后赠送

            if(getcustom('pay_yuanbao')) {
                $total_yuanbao   = 0;//总元宝价格
                $have_no_yuanbao = 0;//是否有非元宝商品
            }

            if($bid)
                $store_info = Db::name('business')->where('aid',aid)->where('id',$bid)->find();
            else
                $store_info = Db::name('admin_set')->where('aid',aid)->find();
            $store_name = $store_info['name'];

            $freightList = \app\model\Freight::getList([['status','=',1],['aid','=',aid],['bid','=',$bid]]);

			$fids = [];
			foreach($freightList as $v){
				$fids[] = $v['id'];
			}
			//			dd($data['prodataList']);
			$extendInput = [];
            $productLimit = [];
            //符合满减的商品价格
            $mj_price = 0;
			$totalweight_price = 0;	
			Db::startTrans();
			foreach($prodata as $key=>$pro){
				$sdata = explode(',',$pro);
				$num = $sdata[2] = intval($sdata[2]);
				if($sdata[2] <= 0) return $this->json(['status'=>0,'msg'=>'购买数量有误']);
				$product = Db::name('shop_product')->where('aid',aid)->where('ischecked',1)->where('bid',$bid)->where('id',$sdata[0])->find();
				if(!$product) return $this->json(['status'=>0,'msg'=>'产品不存在或已下架']);
				
				if($product['status']==0){
					return $this->json(['status'=>0,'msg'=>'商品未上架']);
				}
				if($product['status']==2 && (strtotime($product['start_time']) > time() || strtotime($product['end_time']) < time())){
					return $this->json(['status'=>0,'msg'=>'商品未上架']);
				}
				if($product['status']==3){
					$start_time = strtotime(date('Y-m-d '.$product['start_hours']));
					$end_time = strtotime(date('Y-m-d '.$product['end_hours']));
					if(($start_time < $end_time && ($start_time > time() || $end_time < time())) || ($start_time >= $end_time && ($start_time > time() && $end_time < time()))){
						return $this->json(['status'=>0,'msg'=>'商品未上架']);
					}
				}
				//商品类型数组
				if(!in_array($product['product_type'],$protypes)){
					array_push($protypes,$product['product_type']);
				}
				if(getcustom('shopcate_time')){
					$procids = explode(',',$product['cid']);
					$cate = Db::name('shop_category')->where('aid',aid)->where('id','in',$product['cid'])->select()->toArray();
					foreach($cate as $c){
						if($c['start_hours'] && $c['end_hours']){
							$catestart_time =  strtotime(date('Y-m-d '.$c['start_hours']));
							$cateend_time =  strtotime(date('Y-m-d '.$c['end_hours']));
							if(($catestart_time < $cateend_time && ($catestart_time > time() || $cateend_time < time())) || ($catestart_time >= $cateend_time && ($catestart_time > time() && $cateend_time < time()))){
								return $this->json(['status'=>0,'msg'=>'商品购买时间'.$c['start_hours'].'-'.$c['end_hours'].'请稍后再来']);
							}
						}
					}
				}

                if(getcustom('product_weight')){
                    $productTypes[] = $product['product_type'];
                    $productType = $product['product_type'];
                }

				if($key==0) $title = $product['name'];
				
				$guige = Db::name('shop_guige')->where('id',$sdata[1])->lock(true)->find();
				if(!$guige || $guige['aid'] != aid) return $this->json(['status'=>0,'msg'=>'产品规格不存在或已下架']);
                if(getcustom('shop_yuding')){
                    if($product['stock'] <=0  && $product['yuding_stock'] > 0){
                        Db::name('shop_guige')->where('aid',aid)->where('id',$guige['id'])->update(['stock'=>Db::raw("stock+$num")]);
                        $guige['stock']  = $num;
                    }
                }
				
				if($guige['stock'] < $sdata[2]){
					return $this->json(['status'=>0,'msg'=>'库存不足']);
				}
				$gettj = explode(',',$product['gettj']);
				if(!in_array('-1',$gettj) && !in_array($this->member['levelid'],$gettj) && (!in_array('0',$gettj) || $this->member['subscribe']!=1)){ //不是所有人
					if(!$product['gettjtip']) $product['gettjtip'] = '没有权限购买该商品';
					return $this->json(['status'=>0,'msg'=>$product['gettjtip'],'url'=>$product['gettjurl']]);
				}
				if($product['perlimit'] > 0){
					$buynum = $sdata[2] + Db::name('shop_order_goods')->where('aid',aid)->where('mid',mid)->where('proid',$product['id'])->where('status','in','0,1,2,3')->sum('num');
					if($buynum > $product['perlimit']){
						return $this->json(['status'=>0,'msg'=>'['.$product['name'].'] 每人限购'.$product['perlimit'].'件']);
					}else{
                        if(isset($productLimit[$product['id']])){
                            $productLimit[$product['id']]['buyed'] += $buynum-$sdata[2];;
                            $productLimit[$product['id']]['buy'] += $num;
                        }else{
                            $productLimit[$product['id']]['buyed'] = $buynum-$sdata[2];;
                            $productLimit[$product['id']]['buy'] = $num;
                            $productLimit[$product['id']]['perlimit'] = $product['perlimit'];
                            $productLimit[$product['id']]['name'] = $product['name'];
                        }
                    }
				}
                if($guige['limit_start'] > 0 && $sdata[2] < $guige['limit_start']){
                    return $this->json(['status'=>0,'msg'=>'['.$product['name'].']['.$guige['name'].'] '.$guige['limit_start'].'件起售']);
                }
				$limit_start_state = 1;
				if(getcustom('product_wholesale') && $product['product_type'] == 4){
					$jieti_discount_type = $product['jieti_discount_type'];
					if($jieti_discount_type == 0){
						$limit_start_state = 0;
					}

				}
				if($limit_start_state == 1 && $product['limit_start'] > 0 && $sdata[2] < $product['limit_start']){
					return $this->json(['status'=>0,'msg'=>$product['limit_start'].'件起售']);
				}
				if($product['perlimitdan'] > 0 && $sdata[2] > $product['perlimitdan']){
					return $this->json(['status'=>0,'msg'=>'['.$product['name'].'] 每单限购'.$product['perlimitdan'].'件']);
				}
                if(getcustom('shop_categroy_limit')){
                    //判断分类是否限购
                    if($product['cid']){
                        $category = Db::name('shop_category')->where('id','in',$product['cid'])->where('limit_num', '>',0)->find();
                        if($category) {
                            $product_cid_arr = explode(',',$product['cid']);
                            foreach ($product_cid_arr as $product_cid_arr_v){
                                if($cat_buynum[$product_cid_arr_v])
                                    $cat_buynum[$product_cid_arr_v] += $num;
                                else
                                    $cat_buynum[$product_cid_arr_v] = $num;
                            }
                            if($category['limit_day'] > 0){
                                $buynum = $cat_buynum[$category['id']] + Db::name('shop_order_goods')->where('aid',aid)->where('mid',mid)->whereFindInSet('cid',$category['id'])
                                        ->where('createtime', '>=', time()-$category['limit_day']*86400 )->where('status','in','0,1,2,3')->sum('num');
                                if($buynum > $category['limit_num']){
                                    return $this->json(['status'=>0,'msg'=>'[分类:'.$category['name'].'] 每人每'.$category['limit_day'].'天限购'.$category['limit_num'].'件']);
                                }
                            }else {
                                $buynum = $cat_buynum[$category['id']] + Db::name('shop_order_goods')->where('aid',aid)->where('mid',mid)->whereFindInSet('cid',$product['cid'])
                                        ->where('status','in','0,1,2,3')->sum('num');
                                if($buynum > $category['limit_num']){
                                    return $this->json(['status'=>0,'msg'=>'[分类:'.$category['name'].'] 每人限购'.$category['limit_num'].'件']);
                                }
                            }
                        }
                    }
                }

				if(getcustom('product_memberlevel_limit')){
					if($product['levellimitdata']){
						$rs = \app\model\ShopProduct::memberlevel_limit(aid,mid,$product,$this->member['levelid']);
						if($rs['status']==1 && $rs['limitdata']['ismemberlevel_limit']){
							return $this->json(['status'=>0,'msg'=>$product['name'].$rs['limitdata']['days'].'天内限购'.$rs['limitdata']['limit_num'].'件']);
						}
					}
				}

                if(getcustom('plug_tengrui')) {
                    //判断是否是否符合会员认证、会员关系、一户、用户组
                    $tr_check = new \app\common\TengRuiCheck();
                    $check_product = $tr_check->check_product($this->member,$product);
                    if($check_product && $check_product['status'] == 0){
                        return $this->json(['status'=>$check_product['status'],'msg'=>$check_product['msg']]);
                    }
                    $tr_roomId = $check_product['tr_roomId'];
                    $product['tr_roomId'] = $tr_roomId;
                }
                $guige = $this->formatguige($guige,$product['bid'],$product['lvprice']);
				if(getcustom('product_wholesale') && $product['product_type'] == 4){
					$jieti_num = $num;
					$jieti_discount_type = $product['jieti_discount_type'];
					if($jieti_discount_type == 0){
						foreach($prodata as $kn=>$vn){
							list($proid_n,$ggid_n,$num_n) = explode(',',$vn);
							if($proid_n ==$sdata[0] && $ggid_n !=$sdata[1]){
								$jieti_num +=$num_n;
							}
						}
						if($product['limit_start'] > 0 && $jieti_num < $product['limit_start']){
							return $this->json(['status'=>0,'msg'=>$product['name'].$product['limit_start'].'件起售']);
						}
					}				
					$guige = $this->formatguigewholesale($guige,$product,$jieti_num);
				}
				if(getcustom('plug_xiongmao') && $data['prodataList']) {
				    //自定义价格
                    if($data['prodataList'][$key]['guige']['sell_price'] < $guige['sell_price']) {
                        return $this->json(['status'=>0,'msg'=>'“'.$product['name'].'”不能小于原价']);
                    }
                    $guige['sell_price'] = $data['prodataList'][$key]['guige']['sell_price'];
                }
                if(getcustom('discount_code_zhongchuang') && $product['discount_code_zc'] > 0 && $post['discount_code_zc']){
                    $guige['sell_price'] = $product['discount_code_zc'];
                }
				$product_price += $guige['sell_price'] * $sdata[2];
				if($product['balance']){
					$balance_price += $product_price * $product['balance']*0.01;
					$product_price = $product_price * (1-$product['balance']*0.01);
				}
				$product_priceArr[] = $guige['sell_price'] * $sdata[2];
				if($product['lvprice']==0 && $product['no_discount'] == 0){ //未开启会员价
					$needzkproduct_price += $guige['sell_price'] * $sdata[2];
				}

				if(getcustom('weight_template')){
					$weightids = $data['weightids'];
					if($weightids){
						$sweight = Db::name('shop_weight_template')->where('aid',aid)->where('id',$weightids[$key])->find();
						$weight = $guige['weight'] * $sdata[2];
						$rs = \app\model\Weight::getWeightPrice($sweight,$weight);
						if($rs['status']==0) return $this->json($rs);
						$guige['weight_price'] = $rs['weight_price'];
						$guige['weight_templateid'] = $weightids[$key];
						$totalweight_price += $rs['weight_price'];
					}
				}
				
				$totalweight += $guige['weight'] * $sdata[2];
				$totalnum += $sdata[2];
				
				if($product['scoredkmaxset']==0){
					if($sysset['scoredkmaxpercent'] == 0){
						$scoredkmaxmoney += 0;
					}else{
						if($sysset['scoredkmaxpercent'] > 0 && $sysset['scoredkmaxpercent']<100){
							$scoredkmaxmoney += $sysset['scoredkmaxpercent'] * 0.01 * $guige['sell_price'] * $sdata[2];
						}else{
							$scoredkmaxmoney += $guige['sell_price'] * $sdata[2];
						}
					}
				}elseif($product['scoredkmaxset']==1){
					$scoremaxtype = 1;
					$scoredkmaxmoney += $product['scoredkmaxval'] * 0.01 * $guige['sell_price'] * $sdata[2];
				}elseif($product['scoredkmaxset']==2){
					$scoremaxtype = 1;
					$scoredkmaxmoney += $product['scoredkmaxval'] * $sdata[2];
				}else{
					$scoremaxtype = 1;
					$scoredkmaxmoney += 0;
				}
				$tmpprodata = ['product'=>$product,'guige'=>$guige,'num'=>$sdata[2],'isSeckill'=>0];
                //视力档案
                if(getcustom('product_glass')){
                    $tmpprodata['glass_record_id'] = $sdata[3]??0;
                }
				$prolist[] = $tmpprodata;
				
				if($product['freighttype']==0){
					$fids = array_intersect($fids,explode(',',$product['freightdata']));
				}else{
					$thisfreightList = \app\model\Freight::getList([['status','=',1],['aid','=',aid],['bid','=',$bid]]);
					$thisfids = [];
					foreach($thisfreightList as $v){
						$thisfids[] = $v['id'];
					}
					$fids = array_intersect($fids,$thisfids);
				}
				$proids[] = $product['id'];
				$cids = array_merge($cids,explode(',',$product['cid']));
				if($product['givescore_time'] == 0){
					$givescore += $guige['givescore'] * $sdata[2];
				}else{
					$givescore2 += $guige['givescore'] * $sdata[2];
				}
                if(getcustom('product_givetongzheng')){
                    $givetongzheng += $guige['givetongzheng'] * $sdata[2];
                }

				if($product['to86yk_tid']){
					$extendInput = [['key'=>'input','val1'=>'充值账号','val2'=>'请输入充值账号','val3'=>1],['key'=>'input','val1'=>'确认账号','val2'=>'请再次输入充值账号','val3'=>1]];
				}
                if(getcustom('pay_yuanbao') ){
                    if($product['yuanbao']>0){
                        $total_yuanbao += $product['yuanbao'];
                    }else{
                        $have_no_yuanbao = 1;
                    }
                }
                //如果存在，且数据存在
                if($mjset && $mjdata){
                    //指定分类
                    if($mjset['fwtype']==1){
                        //指定分类数组
                        $cat_arr     = explode(",",$mjset['categoryids']);
                        //商品分类
                        $pro_cat_arr = explode(",",$product['cid']);
                        //交集
                        $j_arr = array_intersect($cat_arr,$pro_cat_arr);
                        if ($j_arr){
                            $mj_price += $guige['sell_price'] * $sdata[2];
                            if($product['balance']){
                                $mj_price = $mj_price * (1-$product['balance']*0.01);
                            }
                        }
                    //指定商品
                    }else if($mjset['fwtype']==2){
                        $pro_arr = explode(",",$mjset['productids']);
                        //商品在指定商品内
                        if (in_array($product['id'], $pro_arr)){
                            $mj_price += $guige['sell_price'] * $sdata[2];
                            if($product['balance']){
                                $mj_price = $mj_price * (1-$product['balance']*0.01);
                            }
                        }
                    }else{
                        $mj_price += $guige['sell_price'] * $sdata[2];
                        if($product['balance']){
                            $mj_price = $mj_price * (1-$product['balance']*0.01);
                        }
                    }
                }
			}
            $givescore = dd_money_format($givescore,$score_weishu);
            $givescore2 = dd_money_format($givescore2,$score_weishu);
			
            //限购判断
            if($productLimit){
                foreach ($productLimit as $pitem){
                    if($pitem['buy']+$pitem['buyed'] > $pitem['perlimit']){
                        return $this->json(['status'=>0,'msg'=>'['.$pitem['name'].'] 每人限购'.$pitem['perlimit'].'件']);
                    }
                }
            }
            //称重商品单独下单
            if(getcustom('product_weight') && array_search('2',$productTypes)!==false && count(array_unique($productTypes))>1){
                return $this->json(['status'=>0,'msg'=>'称重商品须单独下单']);
            }

            if(getcustom('product_handwork')){
	            //手工活类单独下单
	            if(in_array(3,$protypes)){
	            	$ptnum = count($protypes);
	            	if($ptnum>1){
	            		return $this->json(['status'=>0,'msg'=>'手工活类商品请单独购买']);
	            	}
	            }
	        }
			if(!$fids){
				if(count($buydata['prodata'])>1){
					return $this->json(['status'=>0,'msg'=>'所选择商品配送方式不同，请分别下单']);
				}else{
					return $this->json(['status'=>0,'msg'=>'获取配送方式失败']);
				}
			}
			if(getcustom('guige_split')){
				$rs = \app\model\ShopProduct::checkstock($prolist);
				if($rs['status'] == 0) return $this->json($rs);
			}
			//会员折扣
			$leveldk_money = 0;
			if($userlevel && $userlevel['discount']>0 && $userlevel['discount']<10){
				$leveldk_money = $needzkproduct_price * (1 - $userlevel['discount'] * 0.1);
			}
           
			$totalprice = $product_price - $leveldk_money;
            $mj_price   = $mj_price - $leveldk_money;

			$manjian_money = 0;
			$moneyduan = 0;
			if($mjdata && $mj_price>0){
                //如果是累计消费额满减
                if($mjset['total_status']==1){
                    //指定分类
                    if($mjset['fwtype']==1){
                        //查询他分类消费累计
                        $sum_money  = Db::name('shop_order_goods')
                            ->alias('sog')
                            ->join('shop_order so','so.id   = sog.orderid')
                            ->join('shop_product sp','sp.id = sog.proid')
                            ->where('sog.mid',mid)
                            ->where('so.status',3)
                            ->where('sp.cid','in',$mjset['categoryids'])
                            ->sum('sog.totalprice');

                        //分类退款累计
                        $refund_money = Db::name('shop_refund_order_goods')
                            ->alias('srog')
                            ->join('shop_order so','so.id   = srog.orderid')
                            ->join('shop_product sp','sp.id = srog.proid')
                            ->where('srog.mid',mid)
                            ->where('so.status',3)
                            ->where('so.refund_status',2)
                            ->where('sp.cid','in',$mjset['categoryids'])
                            ->sum('srog.refund_money');

                    //指定商品
                    }else if($mjset['fwtype']==2){
                        //查询他商品消费累计
                        $sum_money  = Db::name('shop_order_goods')
                            ->alias('sog')
                            ->join('shop_order so','so.id   = sog.orderid')
                            ->join('shop_product sp','sp.id = sog.proid')
                            ->where('sog.mid',mid)
                            ->where('so.status',3)
                            ->where('sog.proid','in',$mjset['productids'])
                            ->sum('sog.totalprice');

                        //商品退款累计
                        $refund_money = Db::name('shop_refund_order_goods')
                            ->alias('srog')
                            ->join('shop_order so','so.id   = srog.orderid')
                            ->join('shop_product sp','sp.id = srog.proid')
                            ->where('srog.mid',mid)
                            ->where('so.status',3)
                            ->where('so.refund_status',2)
                            ->where('srog.proid','in',$mjset['productids'])
                            ->sum('srog.refund_money');
                    //所有
                    }else{
                        //查询他累计消费多少
                        $sum_money    = Db::name('shop_order')->where('mid',mid)->where('status',3)->sum('totalprice');
                        $refund_money = Db::name('shop_order')->where('mid',mid)->where('status',3)->where('refund_status',2)->sum('refund_money');
                    }
                    $sj_money = $sum_money-$refund_money;
                    $sj_money = round($sj_money,2);
                    $all_price = $sj_money+$mj_price;
                    foreach($mjdata as $give){
                        if($all_price*1 >= $give['money']*1 && $give['money']*1 > $moneyduan){
                            $moneyduan = $give['money']*1;
                            $manjian_money = $give['jian']*1;
                        }
                    }
                }else{
    				foreach($mjdata as $give){
    					if($mj_price*1 >= $give['money']*1 && $give['money']*1 > $moneyduan){
    						$moneyduan = $give['money']*1;
    						$manjian_money = $give['jian']*1;
    					}
    				}
                }
			}
	
			
			if($manjian_money <= 0) $manjian_money = 0;
			$totalprice = $totalprice - $manjian_money;
			if($totalprice < 0) $totalprice = 0;

			//运费
			$freight_price = 0;
			if($data['freight_id']){
				$freight = Db::name('freight')->where('aid',aid)->where('id',$data['freight_id'])->find();
				if($freight['pstype']==11){
					$freight['type11key'] = $data['type11key'];
				}
				if($freight['minpriceset']==1 && $freight['minprice']>0 && $freight['minprice'] > $product_price){
					return $this->json(['status'=>0,'msg'=>$freight['name'] . '满'.$freight['minprice'].'元起送']);
				}
				if(($address['name']=='' || $address['tel'] =='') && ($freight['pstype']==1 || $freight['pstype']==3) && $freight['needlinkinfo']==1){
					return $this->json(['status'=>0,'msg'=>'请填写联系人和联系电话']);
				}
				
				$rs = \app\model\Freight::getFreightPrice($freight,$address,$product_price,$totalnum,$totalweight);
				if($rs['status']==0) return $this->json($rs);
				$freight_price = $rs['freight_price'];
				//判断配送时间选择是否符合要求
				if($freight['pstimeset']==1){
					$freight_times = explode('~',$data['freight_time']);
					if($freight_times[1]){
						$freighttime = strtotime(explode(' ',$freight_times[0])[0] . ' '.$freight_times[1]);
					}else{
						$freighttime = strtotime($freight_times[0]);
					}
					if(time() + $freight['psprehour']*3600 > $freighttime){
						return $this->json(['status'=>0,'msg'=>(($freight['pstype']==0 || $freight['pstype']==2 || $freight['pstype']==10)?'配送':'提货').'时间必须在'.$freight['psprehour'].'小时之后']);
					}
				}
			}elseif($product['freighttype']==3){
				$freight = ['id'=>0,'name'=>'自动发货','pstype'=>3];
				if(getcustom('cefang') && ($address['name']=='' || $address['tel'] =='')){
					return $this->json(['status'=>0,'msg'=>'请填写联系人和联系电话']);
				}
			}elseif($product['freighttype']==4){
				$freight = ['id'=>0,'name'=>'在线卡密','pstype'=>4];
				if(getcustom('cefang') && ($address['name']=='' || $address['tel'] =='')){
					return $this->json(['status'=>0,'msg'=>'请填写联系人和联系电话']);
				}
			}else{
				$freight = ['id'=>0,'name'=>'包邮','pstype'=>0];
			}
			//优惠券
			$new_freight_price = $freight_price;
			$coupon_money = 0;
			$not_used_discount =0;
			if($data['couponrid']){
				if($data['bid'] > 0){
					$business = Db::name('business')->where('aid',aid)->where('id', $data['bid'])->find();
					$bcids = $business['cid'] ? explode(',',$business['cid']) : [];
				}else{
					$bcids = [];
				}
				if($bcids){
					$whereCid = [];
					foreach($bcids as $bcid){
						$whereCid[] = "find_in_set({$bcid},canused_bcids)";
					}
					$whereCids = implode(' or ',$whereCid);
				}else{
					$whereCids = '0=1';
				}
				$couponrids = explode(',',$data['couponrid']);
                $select_prid = [];
				foreach($couponrids as $couponrid){

					$couponrecord = Db::name('coupon_record')->where('aid',aid)->where('mid',mid)->where('id',$couponrid)
						->whereRaw("bid=-1 or bid=".$data['bid']." or (bid=0 and (canused_bids='all' or find_in_set(".$data['bid'].",canused_bids) or ($whereCids)))")->find();
					if(!$couponrecord){
						return $this->json(['status'=>0,'msg'=>'该'.t('优惠券').'不存在']);
					}elseif($couponrecord['status']!=0){
						return $this->json(['status'=>0,'msg'=>'该'.t('优惠券').'已使用过了']);
					}elseif($couponrecord['starttime'] > time()){
						return $this->json(['status'=>0,'msg'=>'该'.t('优惠券').'尚未开始使用']);	
					}elseif($couponrecord['endtime'] < time()){
						return $this->json(['status'=>0,'msg'=>'该'.t('优惠券').'已过期']);	
					}elseif($couponrecord['minprice'] > $totalprice){
						return $this->json(['status'=>0,'msg'=>'该'.t('优惠券').'不符合条件']);
					}elseif($couponrecord['type']!=1 && $couponrecord['type']!=4 && $couponrecord['type']!=10 && $couponrecord['type']!=11){
						return $this->json(['status'=>0,'msg'=>'该'.t('优惠券').'不符合条件']);
					}

					$couponinfo = Db::name('coupon')->where('aid',aid)->where('id',$couponrecord['couponid'])->find();
                    if(empty($couponinfo)){
                        return $this->json(['status'=>0,'msg'=>'该'.t('优惠券').'不存在或已作废']);
                    }
                    //0全场通用,1指定类目,2指定商品
                    if(!in_array($couponinfo['fwtype'],[0,1,2])){
                        return $this->json(['status'=>0,'msg'=>'该'.t('优惠券').'超出可用范围']);
                    }
					if($couponrecord['from_mid']==0 && $couponinfo && $couponinfo['isgive']==2){
						return $this->json(['status'=>0,'msg'=>'该'.t('优惠券').'仅可转赠']);
					}
					$usetj = explode(',',$couponinfo['usetj']);
					if(!in_array('-1',$usetj) && !in_array($this->member['levelid'],$usetj) && (!in_array('0',$usetj) || $this->member['subscribe']!=1)){
						return $this->json(['status'=>0,'msg'=>'该'.t('优惠券').'不可用']);
					}
					if($couponinfo['fwtype']==2){//指定商品可用
						$productids = explode(',',$couponinfo['productids']);
						if(!array_intersect($proids,$productids)){
							return $this->json(['status'=>0,'msg'=>'该'.t('优惠券').'指定商品可用']);
						}
						$thistotalprice = 0;
						foreach($prolist as $k2=>$v2){
							$product = $v2['product'];
							if(in_array($product['id'],$productids)){
								$thistotalprice += $v2['guige']['sell_price'] * $v2['num'];
                                if(getcustom('coupon_xianxia_buy')) {
                                    $couponrecord['sell_price'] = $v2['guige']['sell_price'];
                                }
							}
						}
						if($thistotalprice < $couponinfo['minprice']){
							return $this->json(['status'=>0,'msg'=>'该'.t('优惠券').'指定商品未达到'.$couponinfo['minprice'].'元']);
						}
                        $couponrecord['money'] = min($thistotalprice,$couponrecord['money']);
					}
					if($couponinfo['fwtype']==1){//指定类目可用
						$categoryids = explode(',',$couponinfo['categoryids']);
						$clist = Db::name('shop_category')->where('pid','in',$categoryids)->select()->toArray();
						foreach($clist as $kc=>$vc){
							$categoryids[] = $vc['id'];
							$cate2 = Db::name('shop_category')->where('pid',$vc['id'])->find();
							if($cate2) $categoryids[] = $cate2['id'];
						}
						if(!array_intersect($cids,$categoryids)){
							return $this->json(['status'=>0,'msg'=>'该'.t('优惠券').'指定分类可用']);
						}
						$thistotalprice = 0;
						foreach($prolist as $k2=>$v2){
							$product = $v2['product'];
							if(array_intersect(explode(',',$product['cid']),$categoryids)){
								$thistotalprice += $v2['guige']['sell_price'] * $v2['num'];
							}
						}
						if($thistotalprice < $couponinfo['minprice']){
							return $this->json(['status'=>0,'msg'=>'该'.t('优惠券').'指定分类未达到'.$couponinfo['minprice'].'元']);
						}
                        $couponrecord['money'] = min($thistotalprice,$couponrecord['money']);
					}

					Db::name('coupon_record')->where('id',$couponrid)->update(['status'=>1,'usetime'=>time()]);
					if($couponrecord['type']==4){//运费抵扣券
						$new_freight_price = 0;
					}elseif($couponrecord['type']==10) {//折扣券
                        if(getcustom('coupon_xianxia_buy')){
                            if($couponrecord['is_xianxia_buy'] ==1){
                                $coupon_money += $couponrecord['sell_price'] * (100 - $couponrecord['discount']) * 0.01;
                            }
                        }else{
                            if ($couponinfo['fwtype'] == 1 || $couponinfo['fwtype'] == 2) {
                                $coupon_money += $thistotalprice * (100 - $couponrecord['discount']) * 0.01;
                            } else {
                                $coupon_money += $totalprice * (100 - $couponrecord['discount']) * 0.01;
                            }
                        }
                        if ($coupon_money > $totalprice) $coupon_money = $totalprice;
					}elseif($couponrecord['type']==11){//线下优惠券的兑换券
                        if(getcustom('coupon_xianxia_buy')){
                            foreach($prolist as $k2=>$v2){
                                $product = $v2['product'];
                                $proid = $product['id'];
                                $pronum = $v2['num'];
                                if(in_array($proid,$productids)){
                                    if(!$select_prid[$proid]){
                                        $select_prid[$proid] = 0;
                                    }
                                    if($select_prid[$proid] < $pronum){
                                        $select_prid[$proid] =   $select_prid[$proid] +1;
                                        $coupon_money +=$v2['guige']['sell_price'];
                                        break;
                                    }else{
                                        continue;
                                    }
                                }
                            }
                        }
                    }else{
						$coupon_money += $couponrecord['money'];
						if($coupon_money > $totalprice) $coupon_money = $totalprice;
					}
                    $not_used_discount = $couponinfo['not_used_discount'];
				}
			}
            if(getcustom('coupon_not_used_discount') ){
                $coupon_not_used_discount = Db::name('shop_sysset')->where('aid',aid)->value('coupon_not_used_discount');
                if(($coupon_not_used_discount ==1 || $not_used_discount==1) && $data['couponrid'] ){
                    $totalprice = $totalprice + $leveldk_money;
                    $leveldk_money = 0;
                }
                
            }
			//促销活动
            $cuxiaomoney = 0;
            if(getcustom('multi_promotion')){
                if($data['cuxiaoid']) {
                    foreach ($data['cuxiaoid'] as $cuxiaoid) {
                        if($cuxiaoid > 0){
                            $cuxiaoid = $cuxiaoid;
                            $cuxiaoinfo = Db::name('cuxiao')->where("bid=-1 or bid=".$data['bid'])->where('aid',aid)->where('id',$cuxiaoid)->find();
                            if(!$cuxiaoinfo){
                                return $this->json(['status'=>0,'msg'=>'该促销活动不存在']);
                            }elseif($cuxiaoinfo['starttime'] > time()){
                                return $this->json(['status'=>0,'msg'=>'该促销活动尚未开始']);
                            }elseif($cuxiaoinfo['endtime'] < time()){
                                return $this->json(['status'=>0,'msg'=>'该促销活动已结束']);
                            }elseif($cuxiaoinfo['type']!=5 && $cuxiaoinfo['type']!=6 && $cuxiaoinfo['minprice'] > $totalprice){
                                return $this->json(['status'=>0,'msg'=>'该促销活动不符合条件']);
                            }elseif(($cuxiaoinfo['type']==5 || $cuxiaoinfo['type']==6) && $cuxiaoinfo['minnum'] > $totalnum){
                                return $this->json(['status'=>0,'msg'=>'该促销活动不符合条件']);
                            }
                            if($cuxiaoinfo['fwtype']==2){//指定商品可用
                                $productids = explode(',',$cuxiaoinfo['productids']);
                                if(!array_intersect($proids,$productids)){
                                    return $this->json(['status'=>0,'msg'=>'该促销活动指定商品可用']);
                                } 
                                if($cuxiaoinfo['type']==1 || $cuxiaoinfo['type']==2 || $cuxiaoinfo['type']==3 || $cuxiaoinfo['type']==4){//指定商品是否达到金额要求
                                    $thistotalprice = 0;
                                    foreach($prolist as $k2=>$v2){
                                        $product = $v2['product'];
                                        if(in_array($product['id'],$productids)){
                                            $thistotalprice += $v2['guige']['sell_price'] * $v2['num'];
                                        }
                                    }
                                    if($thistotalprice < $cuxiaoinfo['minprice']){
                                        return $this->json(['status'=>0,'msg'=>'该促销活动指定商品总价未达到'.$cuxiaoinfo['minprice'].'元']);
                                    }
                                }
                                if($cuxiaoinfo['type']==6 || $cuxiaoinfo['type']==5){//指定商品是否达到件数要求
                                    $thistotalnum = 0;
                                    foreach($prolist as $k2=>$v2){
                                        $product = $v2['product'];
                                        if(in_array($product['id'],$productids)){
                                            $thistotalnum += $v2['num'];
                                        }
                                    }
                                    if($thistotalnum < $cuxiaoinfo['minnum']){
                                        return $this->json(['status'=>0,'msg'=>'该促销活动指定商品总数未达到'.$cuxiaoinfo['minnum'].'件']);
                                    }
                                }
                            }
                            if($cuxiaoinfo['fwtype']==1){//指定类目可用
                                $categoryids = explode(',',$cuxiaoinfo['categoryids']);
                                $clist = Db::name('shop_category')->where('pid','in',$categoryids)->select()->toArray();
                                foreach($clist as $kc=>$vc){
                                    $categoryids[] = $vc['id'];
                                    $cate2 = Db::name('shop_category')->where('pid',$vc['id'])->find();
                                    if($cate2) $categoryids[] = $cate2['id'];
                                }
                                if(!array_intersect($cids,$categoryids)){
                                    return $this->json(['status'=>0,'msg'=>'该促销活动指定分类可用']);
                                }
								if($cuxiaoinfo['type']==1 || $cuxiaoinfo['type']==2 || $cuxiaoinfo['type']==3 || $cuxiaoinfo['type']==4){//指定商品是否达到金额要求
                                    $thistotalprice = 0;
                                    foreach($prolist as $k2=>$v2){
                                        $product = $v2['product'];
                                        if(array_intersect(explode(',',$product['cid']),$categoryids)){
                                            $thistotalprice += $v2['guige']['sell_price'] * $v2['num'];
                                        }
                                    }
                                    if($thistotalprice < $cuxiaoinfo['minprice']){
                                        return $this->json(['status'=>0,'msg'=>'该促销活动指定分类总价未达到'.$cuxiaoinfo['minprice'].'元']);
                                    }
                                }
                                if($cuxiaoinfo['type']==6 || $cuxiaoinfo['type']==5){//指定类目内商品是否达到件数要求
                                    $thistotalnum = 0;
                                    foreach($prolist as $k2=>$v2){
                                        $product = $v2['product'];
                                        if(array_intersect(explode(',',$product['cid']),$categoryids)){
                                            $thistotalnum += $v2['num'];
                                        }
                                    }
                                    if($thistotalnum < $cuxiaoinfo['minnum']){
                                        return $this->json(['status'=>0,'msg'=>'该促销活动指定分类总数未达到'.$cuxiaoinfo['minnum'].'件']);
                                    }
                                }
                            }
                            if($cuxiaoinfo['type']==1 || $cuxiaoinfo['type']==6){//满额立减 满件立减
                                $manjian_money = $manjian_money + $cuxiaoinfo['money'];
                                $cuxiaomoney += $cuxiaoinfo['money'] * -1;
                            }elseif($cuxiaoinfo['type']==2){//满额赠送
                                $cuxiaomoney += 0;
                                $product = Db::name('shop_product')->where('aid',aid)->where('id',$cuxiaoinfo['proid'])->find();
                                $guige = Db::name('shop_guige')->where('aid',aid)->where('id',$cuxiaoinfo['ggid'])->find();
                                if(!$product) return $this->json(['status'=>0,'msg'=>'赠送产品不存在']);
                                if(!$guige) return $this->json(['status'=>0,'msg'=>'赠送产品规格不存在']);
                                if($guige['stock'] < 1){
                                    return $this->json(['status'=>0,'msg'=>'赠送产品库存不足']);
                                }
                                $prolist[] = ['product'=>$product,'guige'=>$guige,'num'=>1,'isSeckill'=>0,'gtype'=>1];
                            }elseif($cuxiaoinfo['type']==3){//加价换购
                                $cuxiaomoney += $cuxiaoinfo['money'];
                                $product = Db::name('shop_product')->where('aid',aid)->where('id',$cuxiaoinfo['proid'])->find();
                                $guige = Db::name('shop_guige')->where('aid',aid)->where('id',$cuxiaoinfo['ggid'])->find();
                                if(!$product) return $this->json(['status'=>0,'msg'=>'换购产品不存在']);
                                if(!$guige) return $this->json(['status'=>0,'msg'=>'换购产品规格不存在']);
                                if($guige['stock'] < 1){
                                    return $this->json(['status'=>0,'msg'=>'换购产品库存不足']);
                                }
                                $prolist[] = ['product'=>$product,'guige'=>$guige,'num'=>1,'isSeckill'=>0];
                            }elseif($cuxiaoinfo['type']==4 || $cuxiaoinfo['type']==5){//满额打折 满件打折
                                $cuxiaomoney4 = 0;
                                if($cuxiaoinfo['fwtype']==2){
                                    $prozkArr = array_combine(explode(',',$cuxiaoinfo['productids']),explode(',',$cuxiaoinfo['prozk']));
                                    $pronumArr = array_combine(explode(',',$cuxiaoinfo['productids']),explode(',',$cuxiaoinfo['pronum']));
                                    foreach($prolist as $k=>$v){
                                        $product = $v['product'];
                                        if($prozkArr[$product['id']]){
                                            $prozk = $prozkArr[$product['id']];
                                        }elseif(isset($prozkArr[$product['id']])){
                                            $prozk = $cuxiaoinfo['zhekou'];
                                        }else{
                                            $prozk = 10;
                                        }
                                        if($cuxiaoinfo['type']==5 && $pronumArr[$product['id']] && intval($pronumArr[$product['id']]) > $v['num']){
                                            $prozk = 10;
                                        }
                                        $cuxiaomoney4 += $product_priceArr[$k] * (1 - $prozk * 0.1);
                                    }
                                }elseif($cuxiaoinfo['fwtype']==1) {
                                    //分类
                                    $categoryPrice = 0;
                                    foreach ($prolist as $k2 => $v2) {
                                        $product = $v2['product'];
                                        $cids2 = explode(',', $product['cid']);
                                        if(array_intersect($cids2, $categoryids)) {
                                            $categoryPrice += $v2['guige']['sell_price'] * $v2['num'];
                                        }
                                    }
                                    $cuxiaomoney4 = $categoryPrice * (1 - $cuxiaoinfo['zhekou'] * 0.1);
                                }else{
                                    $cuxiaomoney4 = $totalprice * (1 - $cuxiaoinfo['zhekou'] * 0.1);
                                }
                                $cuxiaomoney4 = round($cuxiaomoney4,2);
                                $manjian_money = $manjian_money + $cuxiaomoney4;
                                $cuxiaomoney += $cuxiaomoney4 * -1;
                            }else{
                                $cuxiaomoney += 0;
                            }
                        }else{
                            $cuxiaomoney += 0;
                        }
                    }
                }
            } else {
                if($data['cuxiaoid'] > 0){
                    $cuxiaoid = $data['cuxiaoid'];
                    $cuxiaoinfo = Db::name('cuxiao')->where("bid=-1 or bid=".$data['bid'])->where('aid',aid)->where('id',$cuxiaoid)->find();
                    if(!$cuxiaoinfo){
                        return $this->json(['status'=>0,'msg'=>'该促销活动不存在']);
                    }elseif($cuxiaoinfo['starttime'] > time()){
                        return $this->json(['status'=>0,'msg'=>'该促销活动尚未开始']);
                    }elseif($cuxiaoinfo['endtime'] < time()){
                        return $this->json(['status'=>0,'msg'=>'该促销活动已结束']);
                    }elseif($cuxiaoinfo['type']!=5 && $cuxiaoinfo['type']!=6 && $cuxiaoinfo['minprice'] > $totalprice){
                        return $this->json(['status'=>0,'msg'=>'该促销活动不符合条件']);
                    }elseif(($cuxiaoinfo['type']==5 || $cuxiaoinfo['type']==6) && $cuxiaoinfo['minnum'] > $totalnum){
                        return $this->json(['status'=>0,'msg'=>'该促销活动不符合条件']);
                    }
                    if($cuxiaoinfo['fwtype']==2){//指定商品可用
                        $productids = explode(',',$cuxiaoinfo['productids']);
                        if(!array_intersect($proids,$productids)){
                            return $this->json(['status'=>0,'msg'=>'该促销活动指定商品可用']);
                        }
						if($cuxiaoinfo['type']==1 || $cuxiaoinfo['type']==2 || $cuxiaoinfo['type']==3 || $cuxiaoinfo['type']==4){//指定商品是否达到金额要求
							$thistotalprice = 0;
							foreach($prolist as $k2=>$v2){
								$product = $v2['product'];
								if(in_array($product['id'],$productids)){
									$thistotalprice += $v2['guige']['sell_price'] * $v2['num'];
								}
							}
							if($thistotalprice < $cuxiaoinfo['minprice']){
								return $this->json(['status'=>0,'msg'=>'该促销活动指定商品未达到'.$cuxiaoinfo['minprice'].'元']);
							}
						}
                        if($cuxiaoinfo['type']==6 || $cuxiaoinfo['type']==5){//指定商品是否达到件数要求
                            $thistotalnum = 0;
                            foreach($prolist as $k2=>$v2){
                                $product = $v2['product'];
                                if(in_array($product['id'],$productids)){
                                    $thistotalnum += $v2['num'];
                                }
                            }
                            if($thistotalnum < $cuxiaoinfo['minnum']){
                                return $this->json(['status'=>0,'msg'=>'该促销活动指定商品未达到'.$cuxiaoinfo['minnum'].'件']);
                            }
                        }
                    }
                    if($cuxiaoinfo['fwtype']==1){//指定类目可用
                        $categoryids = explode(',',$cuxiaoinfo['categoryids']);
                        $clist = Db::name('shop_category')->where('pid','in',$categoryids)->select()->toArray();
                        foreach($clist as $kc=>$vc){
                            $categoryids[] = $vc['id'];
                            $cate2 = Db::name('shop_category')->where('pid',$vc['id'])->find();
                            if($cate2) $categoryids[] = $cate2['id'];
                        }
                        if(!array_intersect($cids,$categoryids)){
                            return $this->json(['status'=>0,'msg'=>'该促销活动指定分类可用']);
                        }
						if($cuxiaoinfo['type']==1 || $cuxiaoinfo['type']==2 || $cuxiaoinfo['type']==3 || $cuxiaoinfo['type']==4){//指定商品是否达到金额要求
							$thistotalprice = 0;
							foreach($prolist as $k2=>$v2){
								$product = $v2['product'];
								if(array_intersect(explode(',',$product['cid']),$categoryids)){
									$thistotalprice += $v2['guige']['sell_price'] * $v2['num'];
								}
							}
							if($thistotalprice < $cuxiaoinfo['minprice']){
								return $this->json(['status'=>0,'msg'=>'该促销活动指定分类未达到'.$cuxiaoinfo['minprice'].'元']);
							}
						}
                        if($cuxiaoinfo['type']==6 || $cuxiaoinfo['type']==5){//指定类目内商品是否达到件数要求
                            $thistotalnum = 0;
                            foreach($prolist as $k2=>$v2){
                                $product = $v2['product'];
                                if(array_intersect(explode(',',$product['cid']),$categoryids)){
                                    $thistotalnum += $v2['num'];
                                }
                            }
                            if($thistotalnum < $cuxiaoinfo['minnum']){
                                return $this->json(['status'=>0,'msg'=>'该促销活动指定分类未达到'.$cuxiaoinfo['minnum'].'件']);
                            }
                        }
                    }
                    if($cuxiaoinfo['type']==1 || $cuxiaoinfo['type']==6){//满额立减 满件立减
                        $manjian_money = $manjian_money + $cuxiaoinfo['money'];
                        $cuxiaomoney = $cuxiaoinfo['money'] * -1;
                    }elseif($cuxiaoinfo['type']==2){//满额赠送
                        $cuxiaomoney = 0;
                        $product = Db::name('shop_product')->where('aid',aid)->where('id',$cuxiaoinfo['proid'])->find();
                        $guige = Db::name('shop_guige')->where('aid',aid)->where('id',$cuxiaoinfo['ggid'])->find();
                        if(!$product) return $this->json(['status'=>0,'msg'=>'赠送产品不存在']);
                        if(!$guige) return $this->json(['status'=>0,'msg'=>'赠送产品规格不存在']);
                        if($guige['stock'] < 1){
                            return $this->json(['status'=>0,'msg'=>'赠送产品库存不足']);
                        }
                        $prolist[] = ['product'=>$product,'guige'=>$guige,'num'=>1,'isSeckill'=>0,'gtype'=>1];
                    }elseif($cuxiaoinfo['type']==3){//加价换购
                        $cuxiaomoney = $cuxiaoinfo['money'];
                        $product = Db::name('shop_product')->where('aid',aid)->where('id',$cuxiaoinfo['proid'])->find();
                        $guige = Db::name('shop_guige')->where('aid',aid)->where('id',$cuxiaoinfo['ggid'])->find();
                        if(!$product) return $this->json(['status'=>0,'msg'=>'换购产品不存在']);
                        if(!$guige) return $this->json(['status'=>0,'msg'=>'换购产品规格不存在']);
                        if($guige['stock'] < 1){
                            return $this->json(['status'=>0,'msg'=>'换购产品库存不足']);
                        }
                        $prolist[] = ['product'=>$product,'guige'=>$guige,'num'=>1,'isSeckill'=>0];
                    }elseif($cuxiaoinfo['type']==4 || $cuxiaoinfo['type']==5){//满额打折 满件打折
                        if($cuxiaoinfo['fwtype']==2){
                            $cuxiaomoney = 0;
                            $prozkArr = array_combine(explode(',',$cuxiaoinfo['productids']),explode(',',$cuxiaoinfo['prozk']));
                            $pronumArr = array_combine(explode(',',$cuxiaoinfo['productids']),explode(',',$cuxiaoinfo['pronum']));
                            foreach($prolist as $k=>$v){
                                $product = $v['product'];
                                if($prozkArr[$product['id']]){
                                    $prozk = $prozkArr[$product['id']];
                                }elseif(isset($prozkArr[$product['id']])){
                                    $prozk = $cuxiaoinfo['zhekou'];
                                }else{
                                    $prozk = 10;
                                }
                                if($cuxiaoinfo['type']==5 && $pronumArr[$product['id']] && intval($pronumArr[$product['id']]) > $v['num']){
                                    $prozk = 10;
                                }
                                $cuxiaomoney += $product_priceArr[$k] * (1 - $prozk * 0.1);
                            }
                        }elseif($cuxiaoinfo['fwtype']==1) {
							//分类
							$categoryPrice = 0;
							foreach ($prolist as $k2 => $v2) {
								$product = $v2['product'];
								$cids2 = explode(',', $product['cid']);
								if(array_intersect($cids2, $categoryids)) {
									$categoryPrice += $v2['guige']['sell_price'] * $v2['num'];
								}
							}
							$cuxiaomoney = $categoryPrice * (1 - $cuxiaoinfo['zhekou'] * 0.1);
						}else{
                            //全部
                            $cuxiaomoney = $totalprice * (1 - $cuxiaoinfo['zhekou'] * 0.1);
                        }
                        $cuxiaomoney = round($cuxiaomoney,2);
                        $manjian_money = $manjian_money + $cuxiaomoney;
                        $cuxiaomoney = $cuxiaomoney * -1;
                    }else{
                        $cuxiaomoney = 0;
                    }
                    if(getcustom('plug_tengrui')) {
                        $tr_check = new \app\common\TengRuiCheck();
                        //判断是否是否符合会员认证、会员关系、一户
                        $check_cuxiao = $tr_check->check_cuxiao($this->member,$cuxiaoinfo);
                        if($check_cuxiao && $check_cuxiao['status'] == 0){
                            $cuxiaomoney = 0;
                        }
                        $cuxiao_tr_roomId = $check_cuxiao['tr_roomId'];
                    }
                }else{
                    $cuxiaomoney = 0;
                }
            }

			$totalprice = $totalprice - $coupon_money + $cuxiaomoney;
			$totalprice = $totalprice + $new_freight_price;

            //发票
            $invoice_money = 0;
            if($store_info['invoice'] && $store_info['invoice_rate'] > 0 && $data['invoice']){
                $invoice_money = round($totalprice * $store_info['invoice_rate'] / 100,2);
                $totalprice = $totalprice + $invoice_money;
            }
			//积分抵扣
			$scoredkscore = 0;
			$scoredk_money = 0;
			if($post['usescore']==1 && $business_selfscore == 0){
				$score2money = $this->sysset['score2money'];
				$scoredkmaxpercent = $this->sysset['scoredkmaxpercent'];
				$scorebdkyf = $this->sysset['scorebdkyf'];
				$scoredk_money = ($this->member['score'] - $alltotalscore) * $score2money;
				if($scorebdkyf == 1){//积分不抵扣运费
					if($scoredk_money > $totalprice - $new_freight_price) $scoredk_money = $totalprice - $new_freight_price;
				}else{
					if($scoredk_money > $totalprice) $scoredk_money = $totalprice;
				}
				if($scoremaxtype == 0){
					if($scoredkmaxpercent >= 0 && $scoredkmaxpercent < 100 && $scoredk_money > 0 && $scoredk_money > $totalprice * $scoredkmaxpercent * 0.01){
						$scoredk_money = $totalprice * $scoredkmaxpercent * 0.01;
					}
				}else{
					if($scoredk_money > $scoredkmaxmoney) $scoredk_money = $scoredkmaxmoney;
				}
				$totalprice = $totalprice - $scoredk_money;
				$totalprice = round($totalprice*100)/100;
				if($scoredk_money > 0){
					$scoredkscore = intval($scoredk_money / $score2money);
				}
			}
			if(getcustom('business_selfscore') && $business_selfscore == 1 && $data['usescore']){
				if($data['bid'] == 0){
					$score2money = $this->sysset['score2money'];
					$scoredkmaxpercent = $this->sysset['scoredkmaxpercent'];
					$scorebdkyf = $this->sysset['scorebdkyf'];
					$scoredk_money = round(($this->member['score'] - $alltotalscore) * $score2money,2);
				}else{
					$business = Db::name('business')->where('id',$data['bid'])->find();
					$memberscore = Db::name('business_memberscore')->where('aid',aid)->where('bid',$data['bid'])->where('mid',mid)->value('score');
					if(!$memberscore) $memberscore = 0;
					$score2money = $business['scoreset']==0 ? $this->sysset['score2money'] : $business['score2money'];
					$scoredkmaxpercent = $business['scoreset']==0 ? $this->sysset['scoredkmaxpercent'] : $business['scoredkmaxpercent'];
					$scorebdkyf = $business['scoreset']==0 ? $this->sysset['score2money'] : $business['scorebdkyf'];
					$scoredk_money = round($memberscore * $score2money,2);
				}
				if($scorebdkyf == 1){//积分不抵扣运费
					if($scoredk_money > $totalprice - $new_freight_price) $scoredk_money = $totalprice - $new_freight_price;
				}else{
					if($scoredk_money > $totalprice) $scoredk_money = $totalprice;
				}
				if($scoremaxtype == 0){
					if($scoredkmaxpercent >= 0 && $scoredkmaxpercent < 100 && $scoredk_money > 0 && $scoredk_money > $totalprice * $scoredkmaxpercent * 0.01){
						$scoredk_money = $totalprice * $scoredkmaxpercent * 0.01;
					}
				}else{
					if($scoredk_money > $scoredkmaxmoney) $scoredk_money = $scoredkmaxmoney;
				}
				$totalprice = $totalprice - $scoredk_money;
				$totalprice = round($totalprice*100)/100;
				if($scoredk_money > 0){
					$scoredkscore = intval($scoredk_money / $score2money);
				}
			}
            if(getcustom('money_dec')){
                if(empty($bid)){
                    $adminset       = Db::name('admin_set')->where('aid',aid)->find();
                    $money_dec_rate = 0;
                    if($adminset && $adminset['money_dec'] && $adminset['money_dec_rate']>0){
                        $money_dec_rate = $adminset['money_dec_rate'];
                    }
                }else{
                    //查询商户余额抵扣比例
                    $business = Db::name('business')->where(['aid'=>aid,'id'=>$bid])->field('money_dec,money_dec_rate')->find();
                    $money_dec_rate = 0;
                    if($business && $business['money_dec'] && $business['money_dec_rate']>0){
                        $money_dec_rate = $business['money_dec_rate'];
                    }
                }

                $dec_money  = 0;//抵扣金额

                //是否使用余额抵扣
                if($data['moneydec_rate']>0 && $money_dec_rate>0){
                    $dec_money  = $totalprice*$money_dec_rate/100;
                    $dec_money  = round($dec_money,2);

                    if($dec_money>= $this->member['money']){
                        $dec_money = $this->member['money'];
                    }
                    $totalprice-= $dec_money;
                }
            }
			if(getcustom('weight_template')){
				$totalprice = $totalprice+$totalweight_price;
			}
			$orderdata = [];
			$orderdata['aid'] = aid;
			$orderdata['mid'] = mid;
			$orderdata['bid'] = $data['bid'];
			if(count($buydata) > 1){
				$orderdata['ordernum'] = $ordernum.'_'.$i;
			}else{
				$orderdata['ordernum'] = $ordernum;
			}
			$orderdata['title'] = $title.(count($prodata)>1?'等':'');
			
			$orderdata['linkman'] = $address['name'];
            $orderdata['company'] = $address['company'];
			$orderdata['tel'] = $address['tel'];
			$orderdata['area'] = $address['area'];
			$orderdata['address'] = $address['address'];
			$orderdata['longitude'] = $address['longitude'];
			$orderdata['latitude'] = $address['latitude'];
			$orderdata['area2'] = $address['province'].','.$address['city'].','.$address['district'];
			
			$orderdata['totalprice'] = $totalprice;
			$orderdata['product_price'] = $product_price;
			$orderdata['leveldk_money'] = $leveldk_money;	//会员折扣
			$orderdata['manjian_money'] = $manjian_money;	//满减活动
			$orderdata['scoredk_money'] = $scoredk_money;	//积分抵扣
			$orderdata['coupon_money'] = $coupon_money + $freight_price - $new_freight_price;	//优惠券抵扣
			$orderdata['scoredkscore'] = $scoredkscore;		//抵扣掉的积分
            $orderdata['balance_price'] = $balance_price;	//尾款金额 定制的
			$orderdata['coupon_rid'] = $data['couponrid'];
			$orderdata['freight_price'] = $freight_price; //运费
            $orderdata['invoice_money'] = $invoice_money; //发票
			$orderdata['givescore'] = $givescore;
			$orderdata['givescore2'] = $givescore2;
            if(getcustom('product_givetongzheng')){
                $orderdata['givetongzheng'] = $givetongzheng;
            }
			if($freight && ($freight['pstype']==0 || $freight['pstype']==10)){ //快递
				$orderdata['freight_text'] = $freight['name'].'('.$freight_price.'元)';
				$orderdata['freight_type'] = $freight['pstype'];
			}elseif($freight && $freight['pstype']==1){ //到店自提
				$orderdata['mdid'] = $data['storeid'];
                if(empty($orderdata['mdid'])){
                    return $this->json(['status'=>0,'msg'=>'请选择门店']);
                }
				$mendian = Db::name('mendian')->where('aid',aid)->where('id',$data['storeid'])->find();
				$orderdata['freight_text'] = $freight['name'].'['.$mendian['name'].']';
				$orderdata['area2'] = $mendian['area'];
				$orderdata['freight_type'] = 1;
				if(getcustom('freight_selecthxbids') && $freight['bid']==0){
					$orderdata['freight_text'] = $freight['name'];
					$orderdata['area2'] = '';
					$orderdata['mdid'] = '-1';
					if(!$orderdata['longitude']){
						$orderdata['longitude'] = $post['longitude'];
						$orderdata['latitude'] = $post['latitude'];
					}
				}
            }elseif($freight && $freight['pstype']==5){ //门店配送
                $orderdata['mdid'] = $data['storeid'];
                $mendian = Db::name('mendian')->where('aid',aid)->where('id',$data['storeid'])->find();
                $orderdata['freight_text'] = $freight['name'].'['.$mendian['name'].']';
                $orderdata['area2'] = $mendian['area'];
                $orderdata['freight_type'] = 5;
			}elseif($freight && $freight['pstype']==2){ //同城配送
                if(getcustom('tongcheng_mendian')){
                    $orderdata['mdid'] = $data['storeid'];
                    $mendian = Db::name('mendian')->where('aid',aid)->where('id',$data['storeid'])->find();
                    $orderdata['freight_text'] = $freight['name'].'['.$mendian['name'].']';
                }else{
                    $orderdata['freight_text'] = $freight['name'].'('.$freight_price.'元)';
                }

				$orderdata['freight_type'] = 2;
			}elseif($freight && $freight['pstype']==12){ //app配送
				$orderdata['freight_text'] = $freight['name'].'('.$freight_price.'元)';
				$orderdata['freight_type'] = 2;
			}elseif($freight && ($freight['pstype']==3 || $freight['pstype']==4)){ //自动发货 在线卡密
				$orderdata['freight_text'] = $freight['name'];
				$orderdata['freight_type'] = $freight['pstype'];
			}elseif($freight && $freight['pstype']==11){ //选择物流配送
				$type11pricedata = json_decode($freight['type11pricedata'],true);
				$orderdata['freight_text'] = $type11pricedata[$freight['type11key']]['name'].'('.$freight_price.'元)';
				$orderdata['freight_type'] = $freight['pstype'];
				$orderdata['freight_content'] = jsonEncode($type11pricedata[$freight['type11key']]);
			}else{
				$orderdata['freight_text'] = '包邮';
			}

			if($sysset['areafenhong_checktype'] == 1 && $orderdata['tel']){
				$addressrs = getaddressfromtel($orderdata['tel']);
				if($addressrs && $addressrs['province']){
					$orderdata['area2'] = $addressrs['province'].','.$addressrs['city'];
				}
			}

			$orderdata['freight_id'] = $freight['id'];
			$orderdata['freight_time'] = $data['freight_time']; //配送时间
			$orderdata['createtime'] = time();
			$orderdata['platform'] = platform;
			$orderdata['hexiao_code'] = random(16);
			$orderdata['hexiao_qr'] = createqrcode(m_url('admin/hexiao/hexiao?type=shop&co='.$orderdata['hexiao_code']));
            if(getcustom('hexiao_member')){
                $orderdata['hexiao_code_member'] = mt_rand(1000,9999);
            }

            if(getcustom('buy_selectmember')){
                if($post['checkmemid']) $orderdata['checkmemid'] = $post['checkmemid'];
            }
            if(getcustom('pay_yuanbao')){
                $orderdata['total_yuanbao']   = $total_yuanbao;
                $orderdata['have_no_yuanbao'] = $have_no_yuanbao;
            }
            if(getcustom('plug_tengrui')){
                $orderdata['manjian_tr_roomId']  = $manjian_tr_roomId?$manjian_tr_roomId:0;
                $orderdata['cuxiao_tr_roomId']   = $cuxiao_tr_roomId?$cuxiao_tr_roomId:0;
                $orderdata['cuxiao_money']       = abs($cuxiaomoney);
                $orderdata['cuxiao_id']          = $cuxiaoid?$cuxiaoid:0;
            }
            if(getcustom('yx_moneypay')){
                $orderdata['cuxiao_money']       = abs($cuxiaomoney);
                $orderdata['cuxiao_id']          = $cuxiaoid?$cuxiaoid:0;
            }
            if(getcustom('shop_yuding')){
                if($product['stock'] <=0 && $product['yuding_stock'] > 0){
                    $orderdata['yuding_type'] = 1;//是否是预定
                }
            }
            if(getcustom('school_product')){
                //升级为当前等级时的操作数据
                $schoolMember = Db::name('school_member')->where('aid',aid)->where('mid',$this->mid)->find();
                if($schoolMember){
                    if($schoolMember['school_id']) {
                        $orderdata['school_id'] = $schoolMember['school_id'];
                        $orderdata['grade_id'] = $schoolMember['grade_id'];
                        $orderdata['class_id'] = $schoolMember['class_id'];
                    }
                    $memberContent = [];
                    if($schoolMember['level_order_id']){
                        $levelOrder = Db::name('member_levelup_order')->where('aid',aid)->where('id',$schoolMember['level_order_id'])->find();
                        //自定义字段是否有值
                        for ($n=0;$n<=20;$n++){
                            $field = 'form'.$n;
                            if(isset($levelOrder[$field]) && !empty($levelOrder[$field])){
                                $fieldValue = $levelOrder[$field];
                                $fieldArr = explode('^_^',$fieldValue);
                                $temp_item = [
                                    'key'=>$fieldArr[2]??'',
                                    'name'=>$fieldArr[0]??'',
                                    'value'=>$fieldArr[1]??''
                                ];
                                $memberContent[] = $temp_item;
                            }
                        }
                    }
                    if($memberContent){
                        $orderdata['member_content'] = json_encode($memberContent);
                    }
                }
            }

            if(getcustom('money_dec')){
                //是否使用余额抵扣
                $orderdata['money_dec_rate'] = $money_dec_rate;//余额抵扣比例
                $orderdata['dec_money']      = $dec_money;//余额抵扣比例
                if($dec_money>0){
                    if($dec_money >$this->member['money']){
                        return $this->json(['status'=>0,'msg'=>t('余额').'不足']);
                    }
                    $res = \app\common\Member::addmoney(aid,mid,-$dec_money,t('余额').'抵扣，订单号: '.$orderdata['ordernum']);
                    if($res['status'] != 1){
                        return $this->json(['status'=>0,'msg'=>t('余额').'抵扣失败']);
                    }
                }
            }
            if(getcustom('shop_buy_worknum')){
				$worknum = $post['worknum']?trim($post['worknum']):'';
	        	$worknum_status = false;
	        	if($freight && $freight['worknum_status']){
	        		$worknum_status = true;
	        	}
	        	if($worknum_status){
	        		if(empty($worknum)){
	        			return $this->json(['status'=>0,'msg'=>'请填写工号']);
	        		}

	        		if(!is_numeric($worknum)){
	        			return $this->json(['status'=>0,'msg'=>'工号必须为10位数字']);
	        		}
	        		$len = strlen($worknum);
	        		if($len != 10){
	        			return $this->json(['status'=>0,'msg'=>'工号必须为10位数字']);
	        		}
		        	
		        	$orderdata['worknum'] = $worknum;//工号
	        	};
	        }
			if(getcustom('weight_template')){
				$orderdata['weight_price'] = $totalweight_price;
			}
            if(getcustom('product_weight') && $productType==2){
                $orderdata['product_type'] = 2;
            }
            if(getcustom('discount_code_zhongchuang') && $post['discount_code_zc']){
                $orderdata['discount_code_zc'] = $post['discount_code_zc'];
            }
            if(getcustom('product_handwork')){
				if($protypes && $protypes[0] == 3){
	        		$orderdata['ishand'] = 1;//是否是手工活商品 0 不是 1：是
	        	}
			}
            $score_weishu = $this->score_weishu;
            if(getcustom('reward_business_score')){
                $orderdata['reward_business_score']= bcmul($product['reward_business_score'],$totalnum,$score_weishu);
            }
			$orderid = Db::name('shop_order')->insertGetId($orderdata);

			\app\model\Freight::saveformdata($orderid,'shop_order',$freight['id'],$data['formdata'],$extendInput);
			$payorderid = \app\model\Payorder::createorder(aid,$orderdata['bid'],$orderdata['mid'],'shop',$orderid,$orderdata['ordernum'],$orderdata['title'],$orderdata['totalprice'],$orderdata['scoredkscore']);

			if($balance_price > 0){
				$balancedata = [];
				$balancedata['aid'] = aid;
				$balancedata['bid'] = $orderdata['bid'];
				$balancedata['mid'] = $orderdata['mid'];
				$balancedata['orderid'] = $orderid;
				$balancedata['ordernum'] = $orderdata['ordernum'];
				$balancedata['title'] = $orderdata['title'];
				$balancedata['money'] = $orderdata['balance_price'];
				$balancedata['type'] = 'balance';
				$balancedata['score'] = 0;
				$balancedata['createtime'] = time();
				$balancedata['status'] = 0;
				$balance_pay_orderid = Db::name('payorder')->insertGetId($balancedata);
				Db::name('shop_order')->where('id',$orderid)->update(['balance_pay_orderid'=>$balance_pay_orderid]);
			}


			if(getcustom('shop_cod_frontpercent') && $shopset['cancod'] == 1 && $shopset['cod_frontpercent'] > 0){
				$frontdata = [];
				$frontdata['aid'] = aid;
				$frontdata['bid'] = $orderdata['bid'];
				$frontdata['mid'] = $orderdata['mid'];
				$frontdata['orderid'] = $orderid;
				$frontdata['ordernum'] = $orderdata['ordernum'].'_0';
				$frontdata['title'] = $orderdata['title'];
				$frontdata['money'] = $orderdata['totalprice'] * $shopset['cod_frontpercent'] * 0.01;
				$frontdata['type'] = 'shopfront';
				$frontdata['score'] = $orderdata['scoredkscore'];
				$frontdata['createtime'] = time();
				$frontdata['status'] = 0;
				$front_pay_orderid = Db::name('payorder')->insertGetId($frontdata);
			}

            //发票
            if($store_info['invoice'] && $data['invoice']) {
                $invoice = [
                    'order_type' => 'shop',
                    'orderid' => $orderid,
                    'ordernum' => $orderdata['ordernum'],
                    'type' => $data['invoice']['invoice_type'] ? $data['invoice']['invoice_type'] : 1,
                    'invoice_name' => $data['invoice']['invoice_name'] ? $data['invoice']['invoice_name'] : '个人',
                    'name_type' => $data['invoice']['name_type'] ? $data['invoice']['name_type'] : 1,
                    'tax_no' => $data['invoice']['tax_no'] ? $data['invoice']['tax_no'] : '',
                    'address' => $data['invoice']['address'] ? $data['invoice']['address'] : '',
                    'tel' => $data['invoice']['tel'] ? $data['invoice']['tel'] : '',
                    'bank_name' => $data['invoice']['bank_name'] ? $data['invoice']['bank_name'] : '',
                    'bank_account' => $data['invoice']['bank_account'] ? $data['invoice']['bank_account'] : '',
                    'mobile' => $data['invoice']['mobile'] ? $data['invoice']['mobile'] : '',
                    'email' => $data['invoice']['email'] ? $data['invoice']['email'] : ''
                ];
                $invoice['aid'] = aid;
                $invoice['bid'] = $orderdata['bid'];
                $invoice['create_time'] = time();
                Db::name('invoice')->insertGetId($invoice);
            }

			$alltotalprice += $orderdata['totalprice'];
			$alltotalscore += $orderdata['scoredkscore'];

			
			//是否是复购
			$hasordergoods = Db::name('shop_order_goods')->where('aid',aid)->where('mid',mid)->where('status','in','1,2,3')->find();
			if($hasordergoods){
				$isfg = 1;
			}else{
				$isfg = 0;
			}

            if(getcustom('everyday_hongbao')) {
                $hd = Db::name('hongbao_everyday')->where('aid', aid)->find();
            }
			$istc1 = 0; //设置了按单固定提成时 只将佣金计算到第一个商品里
			$istc2 = 0;
			$istc3 = 0;
			foreach($prolist as $key=>$v){
				$product = $v['product'];
				$guige = $v['guige'];
				$num = $v['num'];

				$ogdata = [];
				$ogdata['aid'] = aid;
				$ogdata['bid'] = $product['bid'];
				$ogdata['mid'] = mid;
				$ogdata['orderid'] = $orderid;
				$ogdata['ordernum'] = $orderdata['ordernum'];
				$ogdata['proid'] = $product['id'];
				$ogdata['name'] = $product['name'];
				$ogdata['pic'] = $guige['pic']?$guige['pic']:$product['pic'];
				$ogdata['procode'] = $product['procode'];
                $ogdata['barcode'] = $guige['barcode'];
				$ogdata['ggid'] = $guige['id'];
				$ogdata['ggname'] = $guige['name'];
				$ogdata['cid'] = $product['cid'];
				$ogdata['num'] = $num;
				$ogdata['cost_price'] = $guige['cost_price'];
				$ogdata['sell_price'] = $guige['sell_price'];
				$ogdata['totalprice'] = $num * $guige['sell_price'];
                $ogdata['total_weight'] = $num * $guige['weight'];
				if(getcustom('weight_template')){
					$ogdata['weight_price'] = $guige['weight_price'];
					$ogdata['weight_templateid'] = $guige['weight_templateid'];
				}
                if(getcustom('product_weight') && $product['product_type']==2){
                    //称重商品，单价 重量 总价 计算(单位g)
                    $gprice = $guige['sell_price'];
                    $gweight = $guige['weight'];
                    $gtotalweight = $guige['weight']*$num/500;//总重量 斤
                    if($gweight>0){
                        $gpriceNew = $gprice / $gweight * 500;//化成每斤的价格
                    }else{
                        $gpriceNew = $gprice;
                    }
                    $gtotalprice = round($gpriceNew * $gtotalweight,2);
                    $ogdata['sell_price'] = $gpriceNew;
                    $ogdata['real_sell_price'] = $gpriceNew;
                    $ogdata['totalprice'] = $gtotalprice;
                    $ogdata['real_totalprice'] = $gtotalprice;
                    $ogdata['real_total_weight'] = $num * $guige['weight'];
                }
                if(getcustom('fenhong_jiaquan_bylevel')){
                    $ogdata['fenhong_jq_status'] = $product['fenhong_jq_status'];
                }
				$ogdata['status'] = 0;
				$ogdata['createtime'] = time();
				$ogdata['isfg'] = $isfg;
				$ogdata['gtype'] = $v['gtype']??0;

				if($product['isjici'] == 1){
					$ogdata['hexiao_code'] = random(18);
					$ogdata['hexiao_qr'] = createqrcode(m_url('admin/hexiao/hexiao?type=shopproduct&co='.$ogdata['hexiao_code']));
				}else{
                    if(getcustom('product_hexiao_single')){
                        $ogdata['hexiao_code'] = random(18);
                        $ogdata['hexiao_qr'] = createqrcode(m_url('admin/hexiao/hexiao?type=shopproduct&co='.$ogdata['hexiao_code']));
                    }
                }
				if($product['to86yk_tid']){
					$ogdata['to86yk_tid'] = $product['to86yk_tid'];
				}
				if($product['fenhongset'] == 0){ //不参与分红
					$ogdata['isfenhong'] = 2;
				}
				
				$og_totalprice = $ogdata['totalprice'];
				if($product['balance'] > 0){
					$og_totalprice = $og_totalprice * (1 - $product['balance']*0.01);
				}

                $allproduct_price = $product_price;
                $og_leveldk_money = 0;
                $og_coupon_money = 0;
                $og_scoredk_money = 0;
                $og_manjian_money = 0;
                if(getcustom('money_dec')){
                    $og_dec_money  = 0 ;//余额抵扣比例
                }
                if($allproduct_price > 0 && $og_totalprice > 0){
                    if($leveldk_money){
                        $og_leveldk_money = $og_totalprice / $allproduct_price * $leveldk_money;
                    }
                    if($coupon_money){
                        $og_coupon_money = $og_totalprice / $allproduct_price * $coupon_money;
                    }
                    if($scoredk_money){
                        $og_scoredk_money = $og_totalprice / $allproduct_price * $scoredk_money;
                    }
                    if($manjian_money){
                        $og_manjian_money = $og_totalprice / $allproduct_price * $manjian_money;
                    }
                    if(getcustom('money_dec')){
                        if($dec_money){
                            $og_dec_money = $og_totalprice / $allproduct_price * $dec_money;
                        }
                    }
                }
                $ogdata['scoredk_money'] = $og_scoredk_money;
                $ogdata['leveldk_money'] = $og_leveldk_money;
                $ogdata['manjian_money'] = $og_manjian_money;
                $ogdata['coupon_money']  = $og_coupon_money;
                if(getcustom('money_dec')){
                    $ogdata['dec_money'] = $og_dec_money;//余额抵扣比例
                }
                if($product['bid'] > 0) {
					$bset = Db::name('business_sysset')->where('aid',aid)->find();
					$scoredkmoney = $og_scoredk_money ?? 0;
					if($bset['scoredk_kouchu'] == 0){ //扣除积分抵扣
						$scoredkmoney = 0;
					}
                    $leveldkmoney = $og_leveldk_money;
                    if($bset['leveldk_kouchu'] == 0){ //扣除会员折扣
                        $leveldkmoney = 0;
                    }
                    $totalprice_business = $og_totalprice - $og_manjian_money - $og_coupon_money - $scoredkmoney-$leveldkmoney;
                    //商品独立费率
                    if($product['feepercent'] != '' && $product['feepercent'] != null && $product['feepercent'] >= 0) {
                        $ogdata['business_total_money'] = $totalprice_business * (100-$product['feepercent']) * 0.01;
                        if(getcustom('business_deduct_cost')){
                        	if($store_info && $store_info['deduct_cost'] == 1 && $ogdata['cost_price']>0){
                        		if($ogdata['cost_price']<=$ogdata['sell_price']){
									$all_cost_price = $ogdata['cost_price'];
								}else{
									$all_cost_price = $ogdata['sell_price'];
								}
	                        	//扣除成本
			                	$ogdata['business_total_money'] = $totalprice_business - ($totalprice_business-$all_cost_price)*$product['feepercent']/100;
			                }
		                }
						if(getcustom('business_fee_type')){
							$bset = Db::name('business_sysset')->where('aid',aid)->find();
							if($bset['business_fee_type'] == 1){
								$platformMoney = ($totalprice_business-$freight_price) * $product['feepercent'] * 0.01;
								$ogdata['business_total_money'] = $totalprice_business - $platformMoney;
							}elseif($bset['business_fee_type'] == 2){
								$platformMoney = $ogdata['cost_price'] * $product['feepercent'] * 0.01;
								$ogdata['business_total_money'] = $totalprice_business - $platformMoney;
							}
						}
                    } else {
                        //商户费率
//                        $ogdata['business_total_money'] = $totalprice_business * (100-$store_info['feepercent']) * 0.01;
                        if(getcustom('business_deduct_cost')){
                        	if($store_info && $store_info['deduct_cost'] == 1 && $ogdata['cost_price']>0){
                        		if($ogdata['cost_price']<=$ogdata['sell_price']){
									$all_cost_price = $ogdata['cost_price'];
								}else{
									$all_cost_price = $ogdata['sell_price'];
								}
	                        	//扣除成本
			                	$ogdata['business_total_money'] = $totalprice_business - ($totalprice_business-$all_cost_price)*$store_info['feepercent']/100;
			                }
		                }
						if(getcustom('business_fee_type')){
							$bset = Db::name('business_sysset')->where('aid',aid)->find();
							if($bset['business_fee_type'] == 1){
								$platformMoney = $totalprice_business * $store_info['feepercent'] * 0.01;
								$ogdata['business_total_money'] = $totalprice_business - $platformMoney;
							}elseif($bset['business_fee_type'] == 2){
								$platformMoney = $ogdata['cost_price'] * $store_info['feepercent'] * 0.01;
								$ogdata['business_total_money'] = $totalprice_business - $platformMoney;
							}
						}
                    }
                }

				//计算商品实际金额  商品金额 - 会员折扣 - 积分抵扣 - 满减抵扣 - 优惠券抵扣
				if($sysset['fxjiesuantype'] == 1 || $sysset['fxjiesuantype'] == 2){
					$og_totalprice = $og_totalprice - $og_leveldk_money - $og_scoredk_money - $og_manjian_money;
					if($couponrecord['type']!=4) {//运费抵扣券
						$og_totalprice -= $og_coupon_money;
					}
					$og_totalprice = round($og_totalprice,2);
					if($og_totalprice < 0) $og_totalprice = 0;
				}
                if(getcustom('money_dec')){
                    $og_totalprice -= $og_dec_money;//余额抵扣比例
                }
				$ogdata['real_totalprice'] = $og_totalprice; //实际商品销售金额
				
				//计算佣金的商品金额
				$commission_totalprice = $ogdata['totalprice'];
				if($sysset['fxjiesuantype']==1){ //按成交价格
					$commission_totalprice = $ogdata['real_totalprice'];
				}
				$commission_totalpriceCache = $commission_totalprice;
				if($sysset['fxjiesuantype']==2){ //按销售利润
					$commission_totalprice = $ogdata['real_totalprice'] - $guige['cost_price'] * $num;
                }
                if($commission_totalprice < 0) $commission_totalprice = 0;
                if(getcustom('pay_yuanbao')){
                    $ogdata['yuanbao']       = $product['yuanbao'];
                    $ogdata['total_yuanbao'] = $num*$product['yuanbao'];
                }
                if(getcustom('plug_tengrui')) {
                    $ogdata['tr_roomId'] = $product['tr_roomId'];
                }


				if(getcustom('price_dollar')){
					if($shopset['usdrate']>0) $ogdata['usd_sellprice'] = round($guige['sell_price']/$shopset['usdrate'],2);
				}
				if(getcustom('product_handwork')){
					//手工活列商品
					$ogdata['protype'] = $product['product_type'];
		            $ogdata['hand_fee']= $guige['hand_fee'];
		        }
				$ogid = Db::name('shop_order_goods')->insertGetId($ogdata);
                if($product['commissionset']!=-1 && $isfg && getcustom('commission_recursion')){
                    \app\common\Order::recursionCommission(aid,$this->mid,$commission_totalprice,$orderid,$ogid);
                }
                if(getcustom('member_product_price')){
                    if($guige['is_member_product'] ==1){
                        $buylog = [
                            'aid' => aid,
                            'mid' => mid,
                            'ordernum' => $orderdata['ordernum'],
                            'type' =>'shop',
                            'proid' => $ogdata['proid'],
                            'ggid' => $ogdata['ggid'],
                            'orderid' => $orderid,
                            'sell_price' => $ogdata['sell_price'],
                            'num' => $ogdata['num'],
                            'createtime' => time()
                        ];
                        Db::name('member_product_buylog')->insert($buylog);
                    }
                }
                $ogupdate = [];
                if(getcustom('product_glass')){
                    $glass_record_id = $v['glass_record_id'];
                    if($glass_record_id) {
                        $glassrecord = Db::name('glass_record')->where('aid', aid)->where('id', $glass_record_id)->find();
                        if ($glassrecord) {
                            $orderglassrecord = [
                                'glass_record_id' => $glassrecord['id'],
                                'name' => $glassrecord['name'],
                                'desc' => $glassrecord['desc'],
                                'degress_left' => $glassrecord['degress_left'],
                                'degress_right' => $glassrecord['degress_right'],
                                'ipd' => $glassrecord['ipd'],
                                'ipd_left' => $glassrecord['ipd_left'],
                                'ipd_right' => $glassrecord['ipd_right'],
                                'double_ipd' => $glassrecord['double_ipd'],
                                'correction_left' => $glassrecord['correction_left'],
                                'correction_right' => $glassrecord['correction_right'],
                                'is_ats' => $glassrecord['is_ats'],
                                'ats_left' => $glassrecord['ats_left'],
                                'ats_right' => $glassrecord['ats_right'],
                                'ats_zright' => $glassrecord['ats_zright'],
                                'ats_zleft' => $glassrecord['ats_zleft'],
                                'add_right' => $glassrecord['add_right'],
                                'add_left' => $glassrecord['add_left'],
                                'check_time' => $glassrecord['check_time'],
                                'nickname' => $glassrecord['nickname'],
                                'sex' => $glassrecord['sex'],
                                'age' => $glassrecord['age'],
                                'tel' => $glassrecord['tel'],
                                'remark' => $glassrecord['remark'],
                                'type' => $glassrecord['type'],
                                'mid' => $glassrecord['mid'],
                                'createtime' => time(),
                                'aid' => aid,
                                'bid' => $orderdata['bid'] ?? 0,
                                'order_goods_id' => $ogid
                            ];
                            $order_glass_record_id = Db::name('order_glass_record')->insertGetId($orderglassrecord);
                            $ogupdate['glass_record_id'] = $order_glass_record_id;
                        }
                    }
                }
                $parent1 = [];$parent2 = [];$parent3 = [];$parent4 = [];
				$agleveldata = Db::name('member_level')->where('aid',aid)->where('id',$this->member['levelid'])->find();
				if($agleveldata['can_agent'] > 0 && $agleveldata['commission1own']==1){
					$this->member['pid'] = mid;
				}
                if(getcustom('commission_recursion') && $isfg){
                    //首单才发分销奖励
                    $product['commissionset'] = -1;
                }
                if($product['commissionset']!=-1){
                    if(!getcustom('fenxiao_manage')){
                        $sysset['fenxiao_manage_status'] = 0;
                    }
                    if($sysset['fenxiao_manage_status']){
                        $commission_data = \app\common\Fenxiao::fenxiao_jicha($sysset,$this->member,$product,$num,$commission_totalprice);
                    }else{
                        $commission_data = \app\common\Fenxiao::fenxiao($sysset,$this->member,$product,$num,$commission_totalprice,$isfg,$istc1,$istc2,$istc3);
                    }
                    $ogupdate['parent1'] = $commission_data['parent1']??0;
                    $ogupdate['parent2'] = $commission_data['parent2']??0;
                    $ogupdate['parent3'] = $commission_data['parent3']??0;
                    $ogupdate['parent4'] = $commission_data['parent4']??0;
                    $ogupdate['parent1commission'] = $commission_data['parent1commission']??0;
                    $ogupdate['parent2commission'] = $commission_data['parent2commission']??0;
                    $ogupdate['parent3commission'] = $commission_data['parent3commission']??0;
                    $ogupdate['parent4commission'] = $commission_data['parent4commission']??0;
                    $ogupdate['parent1score'] = $commission_data['parent1score']??0;
                    $ogupdate['parent2score'] = $commission_data['parent2score']??0;
                    $ogupdate['parent3score'] = $commission_data['parent3score']??0;
                    $istc1 = $commission_data['istc1']??0;
                    $istc2 = $commission_data['istc2']??0;
                    $istc3 = $commission_data['istc3']??0;
                    if(getcustom('commission_butie')){
                        $butie_data = [];
                        $butie_data['parent1commission_butie'] = $commission_data['parent1commission_butie']??0;
                        $butie_data['parent2commission_butie'] = $commission_data['parent2commission_butie']??0;
                        $butie_data['parent3commission_butie'] = $commission_data['parent3commission_butie']??0;
                    }
                }

                //级差
                if((($product['fx_differential'] == -1 && $sysset['fx_differential'] == 1) || $product['fx_differential'] == 1)  && in_array($product['commissionset'],[1,2,5,6])){
                    if($ogupdate['parent2commission'] > 0) {
                        $cha2_1 = $ogupdate['parent2commission'] - $ogupdate['parent1commission'];
                        $ogupdate['parent2commission'] = $cha2_1 > 0 ? $cha2_1 : 0;
                    }
                    if($ogupdate['parent3commission'] > 0) {
                        $cha3_1 = $ogupdate['parent3commission'] - $ogupdate['parent2commission'] - $ogupdate['parent1commission'];
                        $ogupdate['parent3commission'] = $cha3_1 > 0 ? $cha3_1 : 0;
                    }
                    if(getcustom('commission_butie')){
                        //分销补贴也跟随级差
                        if($butie_data['parent2commission_butie'] > 0) {
                            $cha2_1_butie = $butie_data['parent2commission_butie'] - $butie_data['parent1commission_butie'];
                            $butie_data['parent2commission_butie'] = $cha2_1_butie > 0 ? $cha2_1_butie : 0;
                        }
                        if($butie_data['parent3commission_butie'] > 0) {
                            $cha3_1_butie = $butie_data['parent3commission_butie'] - $butie_data['parent2commission_butie'] - $butie_data['parent1commission_butie'];
                            $butie_data['parent3commission_butie'] = $cha3_1_butie > 0 ? $cha3_1_butie : 0;
                        }
                    }
                }
				//记录产品购车基金和旅游基金数据
                if(getcustom('teamfenhong_gouche')){
                    $ogupdate['gouchebonusset'] = $product['gouchebonusset'];
                    $ogupdate['gouchebonusdata1'] = $product['gouchebonusdata1'];
                    $ogupdate['gouchebonusdata2'] = $product['gouchebonusdata2'];
                }
                if(getcustom('teamfenhong_lvyou')){
                    $ogupdate['lvyoubonusset'] = $product['lvyoubonusset'];
                    $ogupdate['lvyoubonusdata1'] = $product['lvyoubonusdata1'];
                    $ogupdate['lvyoubonusdata2'] = $product['lvyoubonusdata2'];
                }
                //记录产品团队分红伯乐奖参数
                if(getcustom('teamfenhong_bole')){
                    $ogupdate['teamfenhong_bole_bl'] = $product['teamfenhong_bole_bl'];
                    $ogupdate['teamfenhong_bole_money'] = $product['teamfenhong_bole_money'];
                }

                if($ogupdate){
					Db::name('shop_order_goods')->where('id',$ogid)->update($ogupdate);
				}
				
				$totalcommission = 0;
				if($product['commissionset']!=4){
					if(getcustom('plug_ttdz') && $isfg == 1){
						if($ogupdate['parent1'] && ($ogupdate['parent1commission'] || $ogupdate['parent1score'])){
							Db::name('member_commission_record')->insert(['aid'=>aid,'mid'=>$ogupdate['parent1'],'frommid'=>mid,'orderid'=>$orderid,'ogid'=>$ogid,'type'=>'shop','commission'=>$ogupdate['parent1commission'],'score'=>$ogupdate['parent1score'],'remark'=>t('下级').'复购奖励','createtime'=>time()]);
							$totalcommission += $ogupdate['parent1commission'];
						}
						if($ogupdate['parent2'] && ($ogupdate['parent2commission'] || $ogupdate['parent2score'])){
							Db::name('member_commission_record')->insert(['aid'=>aid,'mid'=>$ogupdate['parent2'],'frommid'=>mid,'orderid'=>$orderid,'ogid'=>$ogid,'type'=>'shop','commission'=>$ogupdate['parent2commission'],'score'=>$ogupdate['parent2score'],'remark'=>t('下二级').'复购奖励','createtime'=>time()]);
							$totalcommission += $ogupdate['parent2commission'];
						}
						if($ogupdate['parent3'] && ($ogupdate['parent3commission'] || $ogupdate['parent3score'])){
							Db::name('member_commission_record')->insert(['aid'=>aid,'mid'=>$ogupdate['parent3'],'frommid'=>mid,'orderid'=>$orderid,'ogid'=>$ogid,'type'=>'shop','commission'=>$ogupdate['parent3commission'],'score'=>$ogupdate['parent3score'],'remark'=>t('下三级').'复购奖励','createtime'=>time()]);
							$totalcommission += $ogupdate['parent3commission'];
						}
					}else{
						if($ogupdate['parent1'] && ($ogupdate['parent1commission']>0 || $ogupdate['parent1score']>0)){
						    $data_c = ['aid'=>aid,'mid'=>$ogupdate['parent1'],'frommid'=>mid,'orderid'=>$orderid,'ogid'=>$ogid,'type'=>'shop','commission'=>$ogupdate['parent1commission'],'score'=>$ogupdate['parent1score'],'remark'=>t('下级').'购买商品奖励','createtime'=>time()];
						    if(getcustom('commission_butie')){
                                $data_c['butie'] = $butie_data['parent1commission_butie'];
                                $data_c['commission'] =  bcsub($ogupdate['parent1commission'],$butie_data['parent1commission_butie'],2);
                            }
							Db::name('member_commission_record')->insert($data_c);
							$totalcommission += $ogupdate['parent1commission'];
						}
						if($ogupdate['parent2'] && ($ogupdate['parent2commission']>0 || $ogupdate['parent2score']>0)){
                            $data_c = ['aid'=>aid,'mid'=>$ogupdate['parent2'],'frommid'=>mid,'orderid'=>$orderid,'ogid'=>$ogid,'type'=>'shop','commission'=>$ogupdate['parent2commission'],'score'=>$ogupdate['parent2score'],'remark'=>t('下二级').'购买商品奖励','createtime'=>time()];
                            if(getcustom('commission_butie')){
                                $data_c['butie'] = $butie_data['parent2commission_butie'];
                                $data_c['commission'] =  bcsub($ogupdate['parent2commission'],$butie_data['parent2commission_butie'],2);
                            }
							Db::name('member_commission_record')->insert($data_c);
							$totalcommission += $ogupdate['parent2commission'];
						}
						if($ogupdate['parent3'] && ($ogupdate['parent3commission']>0 || $ogupdate['parent3score']>0)){
                            $data_c = ['aid'=>aid,'mid'=>$ogupdate['parent3'],'frommid'=>mid,'orderid'=>$orderid,'ogid'=>$ogid,'type'=>'shop','commission'=>$ogupdate['parent3commission'],'score'=>$ogupdate['parent3score'],'remark'=>t('下三级').'购买商品奖励','createtime'=>time()];
                            if(getcustom('commission_butie')){
                                $data_c['butie'] = $butie_data['parent3commission_butie'];
                                $data_c['commission'] =  bcsub($ogupdate['parent3commission'],$butie_data['parent3commission_butie'],2);
                            }
							Db::name('member_commission_record')->insert($data_c);
							$totalcommission += $ogupdate['parent3commission'];
						}
                        if($ogupdate['parent4'] && ($ogupdate['parent4commission']>0)){
                            $remark = '持续推荐奖励';
                            if(getcustom('commission_parent_pj_stop')){
                                $remark = '平级奖';
                            }

                            Db::name('member_commission_record')->insert(['aid'=>aid,'mid'=>$ogupdate['parent4'],'frommid'=>mid,'orderid'=>$orderid,'ogid'=>$ogid,'type'=>'shop','commission'=>$ogupdate['parent4commission'],'score'=>0,'remark'=>$remark,'createtime'=>time()]);
							$totalcommission += $ogupdate['parent4commission'];
                        }
					}
					
					if($post['checkmemid'] && $commission_totalprice > 0){
						$checkmember = Db::name('member')->where('aid',aid)->where('id',$post['checkmemid'])->find();
						if($checkmember){
							$buyselect_commission = Db::name('member_level')->where('id',$checkmember['levelid'])->value('buyselect_commission');
							$checkmemcommission = $buyselect_commission * $commission_totalprice * 0.01;
							Db::name('member_commission_record')->insert(['aid'=>aid,'mid'=>$checkmember['id'],'frommid'=>mid,'orderid'=>$orderid,'ogid'=>$ogid,'type'=>'shop','commission'=>$checkmemcommission,'score'=>0,'remark'=>'购买商品时指定奖励','createtime'=>time()]);
						}
					}
				}
				if(getcustom('zhitui_pj')){
				    //直推平级奖
				    $zhitui_pj = json_decode($product['zhitui_pj'],true);
				    $parent = Db::name('member')->where('id',$this->member['pid'])->find();
				    $member_levelid = $this->member['levelid'];
                    $zhitui_pj_commission = $zhitui_pj[$member_levelid]??0;
                    $zhitui_pj_commission = bcmul($zhitui_pj_commission,$num,2);
				    if($parent && $parent['levelid']==$member_levelid && $zhitui_pj_commission>0){
                        Db::name('member_commission_record')->insert(['aid'=>aid,'mid'=>$parent['id'],'frommid'=>mid,'orderid'=>$orderid,'ogid'=>$ogid,'type'=>'shop','commission'=>$zhitui_pj_commission,'score'=>0,'remark'=>'直推平级奖','createtime'=>time()]);
                    }
                }

				
				if($product['commissionset4']==1 && $product['lvprice']==1){ //极差分销
					if(getcustom('jicha_removecommission')){ //算极差时先减去分销的钱
						$commission_totalpriceCache = $commission_totalpriceCache - $totalcommission;
					}
					if($this->member['path']){
						$parentList = Db::name('member')->where('id','in',$this->member['path'])->order(Db::raw('field(id,'.$this->member['path'].')'))->select()->toArray();
						if($parentList){
							$parentList = array_reverse($parentList);
							$lvprice_data = json_decode($guige['lvprice_data'],true);
							$nowprice = $commission_totalpriceCache;
							$giveidx = 0;
							foreach($parentList as $k=>$parent){
								if($parent['levelid'] && $lvprice_data[$parent['levelid']]){
									$thisprice = floatval($lvprice_data[$parent['levelid']]) * $num;
									if($nowprice > $thisprice){
										$commission = $nowprice - $thisprice;
										$nowprice = $thisprice;
										$giveidx++;
										//if($giveidx <=3){
										//	$ogupdate['parent'.$giveidx] = $parent['id'];
										//	$ogupdate['parent'.$giveidx.'commission'] = $commission;
										//}
										Db::name('member_commission_record')->insert(['aid'=>aid,'mid'=>$parent['id'],'frommid'=>mid,'orderid'=>$orderid,'ogid'=>$ogid,'type'=>'shop','commission'=>$commission,'score'=>0,'remark'=>t('下级').'购买商品差价','createtime'=>time()]);
									}
								}
							}
						}
					}
				}

                if(getcustom('product_commission_mid')){
                    if($product['commission_mid']){ //商品指定会员佣金奖励
                        $commission_mid = json_decode($product['commission_mid'],true);
                        $commission_mid = array_filter($commission_mid);
                        foreach($commission_mid as $kcm=>$vcm){
                            $money=0;
                            if($vcm['mid'] && ($vcm['percent']>0 || $vcm['money']>0)){
                                if($vcm['percent']>0) $money += $commission_totalprice*$vcm['percent']/100;
                                $money += $vcm['money'] * $num;
                                $money = round($money,2);
                                if($money > 0){
                                    Db::name('member_commission_record')->insert(['aid'=>aid,'mid'=>$vcm['mid'],'frommid'=>mid,'orderid'=>$orderid,'ogid'=>$ogid,'type'=>'shop','commission'=>$money,'score'=>0,'remark'=>$vcm['name'],'createtime'=>time()]);
                                }
                            }
                        }
                    }
                }

				if(getcustom('commission_givedown')){
					$commission_recordlist = Db::name('member_commission_record')->field("mid,sum(commission) totalcommission")->where('aid',aid)->where('orderid',$orderid)->where('ogid',$ogid)->where('type','shop')->where('commission','>',0)->group('mid')->select()->toArray();
					foreach($commission_recordlist as $record){
						$thismember = Db::name('member')->where('id',$record['mid'])->find();
						$memberlevel = Db::name('member_level')->where('id',$thismember['levelid'])->find();
						if($memberlevel && ($memberlevel['givedown_percent'] > 0 || $memberlevel['givedown_commission'] > 0)){
							$downmemberlist = Db::name('member')->where('aid',aid)->where('pid',$record['mid'])->select()->toArray();
							if(!$downmemberlist) continue;
							$downcommission = $memberlevel['givedown_commission'] / count($downmemberlist) + $record['totalcommission'] * $memberlevel['givedown_percent'] * 0.01 / count($downmemberlist);
							foreach($downmemberlist as $downmember){
								Db::name('member_commission_record')->insert(['aid'=>aid,'mid'=>$downmember['id'],'frommid'=>$thismember['id'],'orderid'=>$orderid,'ogid'=>$ogid,'type'=>'shop','commission'=>$downcommission,'score'=>0,'remark'=>$memberlevel['givedown_txt'],'createtime'=>time()]);
							}
						}
					}
				}

				if(getcustom('everyday_hongbao')) {
                    $hongbaoEdu = 0;
                    if($product['everyday_hongbao_bl'] === null) {
                        $hongbaoEdu = $og_totalprice * $hd['shop_product_hongbao_bl'] / 100;
                    } elseif($product['everyday_hongbao_bl'] > 0 ) {
                        $hongbaoEdu = $og_totalprice * $product['everyday_hongbao_bl'] / 100;
                    }
                    $hongbaoEdu = round($hongbaoEdu,2);
                    if($hongbaoEdu > 0)
                        Db::name('shop_order_goods')->where('id',$ogid)->update(['hongbaoEdu' => $hongbaoEdu]);
                }
                
				//删除购物车
				Db::name('shop_cart')->where('aid',aid)->where('mid',mid)->where('ggid',$guige['id'])->where('proid',$product['id'])->delete();
				//减库存
                
                if(getcustom('shop_yuding')){
                    if($product['stock'] <=0  && $product['yuding_stock'] > 0){
                        Db::name('shop_product')->where('aid',aid)->where('id',$product['id'])->update(['stock'=>Db::raw("stock+$num"),'yuding_stock'=>Db::raw("yuding_stock-$num")]);
                    }
                }
               
				Db::name('shop_guige')->where('aid',aid)->where('id',$guige['id'])->update(['stock'=>Db::raw("stock-$num"),'sales'=>Db::raw("sales+$num")]);
				Db::name('shop_product')->where('aid',aid)->where('id',$product['id'])->update(['stock'=>Db::raw("stock-$num"),'sales'=>Db::raw("sales+$num")]);
				if(getcustom('guige_split')){
					\app\model\ShopProduct::declinkstock($product['id'],$guige['id'],$num);
				}
			}
			Db::commit();

			if(getcustom('jushuitan')){
				if($sysset['jushuitankey']){
					//创建聚水潭订单
					$order = Db::name('shop_order')->where('aid',aid)->where('id',$orderid)->find();
					$rs = \app\custom\jushuitan::create($order,'WAIT_BUYER_PAY');
					if(!$rs['code']){
						$ordernum = $rs['data']['datas']['so_id'];
						//修改订单的聚水潭内部单号
					}
				}
			}

			if(platform == 'toutiao'){
				\app\common\Ttpay::build(aid,mid,$orderdata['title'],$orderdata['ordernum'],$orderdata['totalprice'],'shop');
				\app\common\Ttpay::pushorder(aid,$orderdata['ordernum'],0);
			}

			//公众号通知 订单提交成功
			$tmplcontent = [];
			$tmplcontent['first'] = '有新订单提交成功';
			$tmplcontent['remark'] = '点击进入查看~';
			$tmplcontent['keyword1'] = $store_name; //店铺
			$tmplcontent['keyword2'] = date('Y-m-d H:i:s',$orderdata['createtime']);//下单时间
			$tmplcontent['keyword3'] = $orderdata['title'];//商品
			$tmplcontent['keyword4'] = $orderdata['totalprice'].'元';//金额
            $tempconNew = [];
            $tempconNew['character_string2'] = $orderdata['ordernum'];//订单号
            $tempconNew['thing8'] = $store_name;//门店名称
            $tempconNew['thing3'] = $orderdata['title'];//商品名称
            $tempconNew['amount7'] = $orderdata['totalprice'];//金额
            $tempconNew['time4'] = date('Y-m-d H:i:s',$orderdata['createtime']);//下单时间
			\app\common\Wechat::sendhttmpl(aid,$orderdata['bid'],'tmpl_orderconfirm',$tmplcontent,m_url('admin/order/shoporder'),$orderdata['mdid'],$tempconNew);
			
			$tmplcontent = [];
			$tmplcontent['thing11'] = $orderdata['title'];
			$tmplcontent['character_string2'] = $orderdata['ordernum'];
			$tmplcontent['phrase10'] = '待付款';
			$tmplcontent['amount13'] = $orderdata['totalprice'].'元';
			$tmplcontent['thing27'] = $this->member['nickname'];
			\app\common\Wechat::sendhtwxtmpl(aid,$orderdata['bid'],'tmpl_orderconfirm',$tmplcontent,'admin/order/shoporder',$orderdata['mdid']);
		}
		if(count($buydata) > 1){ //创建合并支付单
			$payorderid = \app\model\Payorder::createorder(aid,0,mid,'shop_hb',$orderid,$ordernum,$orderdata['title'],$alltotalprice,$alltotalscore);
		}

		return $this->json(['status'=>1,'payorderid'=>$payorderid,'msg'=>'提交成功']);
	}
	public function memberSearch(){
		$search = input('param.diqu');
		if($search){
			$search = explode(',',$search);
			$where = [];
			$where[] = ['aid','=',aid];
			if($search[0]!='全部'){
				$where[]=['province','=',$search[0]];
			}
			if($search[1]!='全部'){
				$where[]=['city','=',$search[1]];
			}
			if($search[2]!='全部'){
				$where[]=['area','=',$search[2]];
			}
			
			$levelids = Db::name('member_level')->where('aid',aid)->where('can_buyselect',1)->column('id');
			$where[]=['levelid','in',$levelids];
			$memberList = db('member')->field('id,nickname,headimg,tel,province,city,area')->where($where)->select();
			if(count($memberList) > 0 ){
				return $this->json(['status'=>1,'memberList'=>$memberList,'msg'=>'获取成功']);
			}else{
				return $this->json(['status'=>0,'memberList'=>[],'msg'=>'无符合条件用户']);
			}
		}
	}
	//获取升级申请时填写的资料
	public function getmemberuplvinfo(){
		$mid = input('post.mid');
		$minfo = Db::name('member')->where('id',$mid)->find();
		$info = Db::name('member_levelup_order')->where('mid',$mid)->where('status',2)->find();
		$data = [['昵称',$minfo['nickname']]];
		for($i=0;$i<=20;$i++){
			if($info['form'.$i]){
				$thisdata = explode('^_^',$info['form'.$i]);
				if($thisdata[1]!==''){
					if($thisdata[0] == '手机号'){
						$thisdata[1] = substr($thisdata[1],0,3).'*****'.substr($thisdata[1],-3);
					}
					$data[] = $thisdata;
				}
			}
		}
		return json(['info'=>$data]);
	}

	public function mendian()
    {
        $id = input('param.id');
        $info = Db::name('mendian')->where('aid',aid)->where('id',$id)->find();
        if(empty($info)) {
            return $this->json(['status'=>0,'msg'=>'门店不存在']);
        }
        if($info['status'] != 1) {
            return $this->json(['status'=>0,'msg'=>'门店未开启']);
        }

        if($info['pics']) $info['pics'] = $info['pics'] ? explode(',', $info['pics']) : [];

        return json(['status'=>1,'info'=>$info]);
    }

    public function invoice()
    {
        $bid = input('param.bid/d');
        $post = input('post.');
        $order_type = input('param.type');

        if($bid) {
            $invoice = Db::name('business')->where('aid',aid)->where('id',$bid)->find();
        } else {
            $invoice = Db::name('admin_set')->where('aid',aid)->find();
        }
        if(!$invoice['invoice']) return $this->json(['status'=>0,'msg'=>'未开启发票功能']);

        //todo 上次的信息
        $rdata['status'] = 1;
        $rdata['invoice_type'] = explode(',', $invoice['invoice_type']);
        return $this->json($rdata);
    }

	//获取抖音商品id
	public function getDouyinProductId(){
		$proid = input('param.proid/d');
		$douyin_product_id = Db::name('shop_product')->where('id',$proid)->value('douyin_product_id');
		if(!$douyin_product_id) return $this->json(['status'=>0,'msg'=>'获取失败']);
		return $this->json(['status'=>1,'douyin_product_id'=>$douyin_product_id]);
	}

	public function imgsearch(){
        if(getcustom('image_search')){
            $image_search = Db::name('admin')->where('id',aid)->value('image_search');
            if($image_search == 1) {
                $set = Db::name('baidu_set')->where('aid',aid)->find();
                if($set['image_search'] == 1){
                    $rdata['status'] = 1;
                    $rdata['info']['banner'] = $set['image_search_banner'];
                    $rdata['info']['image_search_pic'] = $set['image_search_pic'];
                    return $this->json($rdata);
                }
            }
        }
        return $this->json(['status' => 0, 'msg' => '系统错误']);
    }

    public function getprolistWithImg(){
        if(getcustom('image_search')) {
            $where = [];
            $where[] = ['aid', '=', aid];
            $where[] = ['ischecked', '=', 1];
            if (isdouyin == 1) {
                $where[] = ['douyin_product_id', '<>', ''];
            } else {
                $where[] = ['douyin_product_id', '=', ''];
            }

            //图搜
            $imgurl = input('param.imgurl');
            if ($imgurl) {
                $client = new \app\custom\Baidu(aid);
                $proids = $client->searchProduct($imgurl);
                if ($proids) {
                    $where[] = ['id', 'in', $proids];
                    $proids = implode(',', $proids);
                    $order = Db::raw('field(id,' . $proids . ')');
                } else {
                    $order = 'sort desc,id desc';
                }
            }
            if (input('param.field') && input('param.order')) {
                $order = input('param.field') . ' ' . input('param.order') . ',sort,id desc';
            }

            //$where[] = ['status','=',1];
            $nowtime = time();
            $nowhm = date('H:i');
            $where[] = Db::raw("`status`=1 or (`status`=2 and unix_timestamp(start_time)<=$nowtime and unix_timestamp(end_time)>=$nowtime) or (`status`=3 and ((start_hours<end_hours and start_hours<='$nowhm' and end_hours>='$nowhm') or (start_hours>=end_hours and (start_hours<='$nowhm' or end_hours>='$nowhm'))) )");

            if (input('param.bid')) {
                $where[] = ['bid', '=', input('param.bid/d')];
            } else {
                $business_sysset = Db::name('business_sysset')->where('aid', aid)->find();
                if (!$business_sysset || $business_sysset['status'] == 0 || $business_sysset['product_isshow'] == 0) {
                    $where[] = ['bid', '=', 0];
                }
            }
            //分类
            if (input('param.cid')) {
                $cid = input('post.cid') ? input('post.cid/d') : input('param.cid/d');
                $where2 = "find_in_set('-1',showtj)";
                if ($this->member) {
                    $where2 .= " or find_in_set('" . $this->member['levelid'] . "',showtj)";
                    if ($this->member['subscribe'] == 1) {
                        $where2 .= " or find_in_set('0',showtj)";
                    }
                }
                $tjwhere[] = Db::raw($where2);
                //子分类
                $clist = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->where('pid', $cid)->column('id');
                if ($clist) {
                    $clist2 = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->where('pid', 'in', $clist)->column('id');
                    $cCate = array_merge($clist, $clist2, [$cid]);
                    if ($cCate) {
                        $whereCid = [];
                        foreach ($cCate as $k => $c2) {
                            $whereCid[] = "find_in_set({$c2},cid)";
                        }
                        $where[] = Db::raw(implode(' or ', $whereCid));
                    }
                } else {
                    $where[] = Db::raw("find_in_set(" . $cid . ",cid)");
                }
            } else {
                if (getcustom('product_cat_showtj')) {
                    $where2 = "find_in_set('-1',showtj)";
                    if ($this->member) {
                        $where2 .= " or find_in_set('" . $this->member['levelid'] . "',showtj)";
                        if ($this->member['subscribe'] == 1) {
                            $where2 .= " or find_in_set('0',showtj)";
                        }
                    }
                    $tjwhere[] = Db::raw($where2);
                    $clist = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->column('id');

                    if ($clist) {
                        $whereCid = [];
                        foreach ($clist as $k => $c2) {
                            $whereCid[] = "find_in_set({$c2},cid)";
                        }
                        $where[] = Db::raw(implode(' or ', $whereCid));
                    }
                }
            }

            //商家的商品分类
            if (input('param.cid2')) {
                $cid2 = input('post.cid2') ? input('post.cid2/d') : input('param.cid2/d');
                //子分类
                $clist = Db::name('shop_category2')->where('aid', aid)->where('pid', $cid2)->column('id');
                if ($clist) {
                    $clist2 = Db::name('shop_category2')->where('aid', aid)->where('pid', 'in', $clist)->column('id');
                    $cCate = array_merge($clist, $clist2, [$cid2]);
                    if ($cCate) {
                        $whereCid = [];
                        foreach ($cCate as $k => $c2) {
                            $whereCid[] = "find_in_set({$c2},cid2)";
                        }
                        $where[] = Db::raw(implode(' or ', $whereCid));
                    }
                } else {
                    $where[] = Db::raw("find_in_set(" . $cid2 . ",cid2)");
                }
            }
            if (input('param.gid')) $where[] = Db::raw("find_in_set(" . intval(input('param.gid')) . ",gid)");
            if (input('param.keyword')) {
                $where[] = ['name', 'like', '%' . input('param.keyword') . '%'];
            }

            $where2 = "find_in_set('-1',showtj)";
            if ($this->member) {
                $where2 .= " or find_in_set('" . $this->member['levelid'] . "',showtj)";
                if ($this->member['subscribe'] == 1) {
                    $where2 .= " or find_in_set('0',showtj)";
                }
            }
            $where[] = Db::raw($where2);

            //优惠券可用商品列表
            $cpid = input('param.cpid/d');
            if ($cpid > 0) {
                $coupon = Db::name('coupon')->where('id', $cpid)->find();
                $where[] = ['bid', '=', $coupon['bid']];
                if ($coupon['fwtype'] == 1) { //指定类目
                    $categoryids = explode(',', $coupon['categoryids']);
                    $clist = Db::name('shop_category')->where('pid', 'in', $categoryids)->select()->toArray();
                    foreach ($clist as $kc => $vc) {
                        $categoryids[] = $vc['id'];
                        $cate2 = Db::name('shop_category')->where('pid', $vc['id'])->find();
                        $categoryids[] = $cate2['id'];
                    }
                    $whereCid = [];
                    foreach ($categoryids as $k => $c2) {
                        $whereCid[] = "find_in_set({$c2},cid)";
                    }
                    $where[] = Db::raw(implode(' or ', $whereCid));
                }
                if ($coupon['fwtype'] == 2) { //指定商品
                    $where[] = ['id', 'in', $coupon['productids']];
                }
            }
            $pernum = 10;
            $pagenum = input('post.pagenum');
            if (!$pagenum) $pagenum = 1;

            $datalist = Db::name('shop_product')->field("id,pic,name,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint,price_type")->where($where)/*->page($pagenum,$pernum)*/ ->order($order)->select()->toArray();
//		$sql = Db::getlastsql();dd($sql);
            if (!$datalist) $datalist = [];
            $datalist = $this->formatprolist($datalist);

            
            return $this->json(['status' => 1, 'data' => $datalist]);
        }
    }

    public function diylight(){
        if(getcustom('diy_light')){
            $set = Db::name('diylight_set')->where('aid',aid)->where('mid',0)->find();
            if($set['status'] == 1){
                $rdata['status'] = 1;
                $set['bgimgs'] = $set['bgimgs'] ? explode(',',$set['bgimgs']) : [];

                if(mid > 0) {
                    $userimg = Db::name('diylight_set')->where('aid',aid)->where('mid',mid)->order('id','desc')->column('bgimgs');
                    if($userimg && $set['bgimgs']){
                        $set['bgimgs'] = array_merge($userimg,$set['bgimgs']);
                    }
                }

                $rdata['data']['bgimgs'] = $set['bgimgs'];
                $proid = input('param.id/d');
                if($proid) {
                    $pro = $this->getprolistForLight($proid);
                    $pro = $pro->getData();
                    $rdata['data']['pro'] = $pro['data'][0];
                }

                return $this->json($rdata);
            }
        }
        return $this->json(['status' => 0, 'msg' => '系统错误']);
    }
    public function diylightUpload(){
	    $this->checklogin();
	    $imgurl = input('param.imgurl');
	    if(empty($imgurl)){
            return $this->json(['status' => 0, 'msg' => '请上传图片']);
        }
        if(getcustom('diy_light')){
            $set = Db::name('diylight_set')->where('aid',aid)->where('mid',0)->find();
            if($set['status'] == 1){
                $userimg = Db::name('diylight_set')->where('aid',aid)->where('mid',mid)->order('id','asc')->select()->toArray();
                $data = [
                    'aid'=>aid,
                    'mid'=>mid,
                    'bgimgs' =>$imgurl
                ];
                Db::name('diylight_set')->insert($data);
                if(count($userimg) >= 5){
                    Db::name('diylight_set')->where('id',$userimg[0]['id'])->delete();
                }

                return $this->json(['status' => 1, 'msg' => 'ok']);
            }
        }
        return $this->json(['status' => 0, 'msg' => '系统错误']);
    }

    public function getprolistForLight($proid=0){
        $where = [];
        $where[] = ['aid','=',aid];
        $where[] = ['ischecked','=',1];
        $where[] = ['diypics','<>',''];
        if(isdouyin == 1){
            $where[] = ['douyin_product_id','<>',''];
        }else{
            $where[] = ['douyin_product_id','=',''];
        }
        //$where[] = ['status','=',1];
        $nowtime = time();
        $nowhm = date('H:i');
        $where[] = Db::raw("`status`=1 or (`status`=2 and unix_timestamp(start_time)<=$nowtime and unix_timestamp(end_time)>=$nowtime) or (`status`=3 and ((start_hours<end_hours and start_hours<='$nowhm' and end_hours>='$nowhm') or (start_hours>=end_hours and (start_hours<='$nowhm' or end_hours>='$nowhm'))) )");

        if(input('param.bid')){
            $where[] = ['bid','=',input('param.bid/d')];
        }else{
            $business_sysset = Db::name('business_sysset')->where('aid',aid)->find();
            if(!$business_sysset || $business_sysset['status']==0 || $business_sysset['product_isshow']==0){
                $where[] = ['bid','=',0];
            }
        }
        if(input('param.field') && input('param.order')){
            $order = input('param.field').' '.input('param.order').',sort,id desc';
        }else{
            $order = 'sort desc,id desc';
        }
        //分类
        if(input('param.cid')){
            $cid = input('post.cid') ? input('post.cid/d') : input('param.cid/d');
            $where2 = "find_in_set('-1',showtj)";
            if($this->member){
                $where2 .= " or find_in_set('".$this->member['levelid']."',showtj)";
                if($this->member['subscribe']==1){
                    $where2 .= " or find_in_set('0',showtj)";
                }
            }
            $tjwhere[] = Db::raw($where2);
            //子分类
            $clist = Db::name('shop_category')->where($tjwhere)->where('aid',aid)->where('pid',$cid)->column('id');
            if($clist){
                $clist2 = Db::name('shop_category')->where($tjwhere)->where('aid',aid)->where('pid','in',$clist)->column('id');
                $cCate = array_merge($clist, $clist2, [$cid]);
                if($cCate){
                    $whereCid = [];
                    foreach($cCate as $k => $c2){
                        $whereCid[] = "find_in_set({$c2},cid)";
                    }
                    $where[] = Db::raw(implode(' or ',$whereCid));
                }
            } else {
                $where[] = Db::raw("find_in_set(".$cid.",cid)");
            }
        } else {
            if(getcustom('product_cat_showtj')) {
                $where2 = "find_in_set('-1',showtj)";
                if($this->member){
                    $where2 .= " or find_in_set('".$this->member['levelid']."',showtj)";
                    if($this->member['subscribe']==1){
                        $where2 .= " or find_in_set('0',showtj)";
                    }
                }
                $tjwhere[] = Db::raw($where2);
                $clist = Db::name('shop_category')->where($tjwhere)->where('aid',aid)->column('id');
                if($clist){
                    $whereCid = [];
                    foreach($clist as $k => $c2){
                        $whereCid[] = "find_in_set({$c2},cid)";
                    }
                    $where[] = Db::raw(implode(' or ',$whereCid));
                }
            }
        }

        //商家的商品分类
        if(input('param.cid2')){
            $cid2 = input('post.cid2') ? input('post.cid2/d') : input('param.cid2/d');
            //子分类
            $clist = Db::name('shop_category2')->where('aid',aid)->where('pid',$cid2)->column('id');
            if($clist){
                $clist2 = Db::name('shop_category2')->where('aid',aid)->where('pid','in',$clist)->column('id');
                $cCate = array_merge($clist, $clist2, [$cid2]);
                if($cCate){
                    $whereCid = [];
                    foreach($cCate as $k => $c2){
                        $whereCid[] = "find_in_set({$c2},cid2)";
                    }
                    $where[] = Db::raw(implode(' or ',$whereCid));
                }
            } else {
                $where[] = Db::raw("find_in_set(".$cid2.",cid2)");
            }
        }
        if(input('param.gid')) $where[] = Db::raw("find_in_set(".intval(input('param.gid')).",gid)");
        if(input('param.keyword')){
            $where[] = ['name','like','%'.input('param.keyword').'%'];
        }
        if($proid){
            $where[] = ['id','=',$proid];
        }

        $where2 = "find_in_set('-1',showtj)";
        if($this->member){
            $where2 .= " or find_in_set('".$this->member['levelid']."',showtj)";
            if($this->member['subscribe']==1){
                $where2 .= " or find_in_set('0',showtj)";
            }
        }
        $where[] = Db::raw($where2);

        $pernum = 9;
        $pagenum = input('post.pagenum');
        if(!$pagenum) $pagenum = 1;
        $datalist = Db::name('shop_product')->field("id,pic,name,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint,price_type,diypics")->where($where)->page($pagenum,$pernum)->order($order)->select()->toArray();
//		$sql = Db::getlastsql();dd($sql);
        if(!$datalist) $datalist = [];
        else {
            foreach ($datalist as $k => $item){
                $datalist[$k]['url'] = $item['diypics'] ? explode(',',$item['diypics']) : [];
                $datalist[$k]['realUrl'] = $datalist[$k]['url'] ? $datalist[$k]['url'][0] : '';
            }
        }

        return $this->json(['status'=>1,'data'=>$datalist]);
    }


	function getwxScheme(){
		$this->checklogin();
		$post = input('post.');
		$platform = platform;
		$page = '/pages/shop/product';
		$scene = 'id='.$post['proid'].'&pid='.$this->member['id'];
		$product = Db::name('shop_product')->where('id',$post['proid'])->find();
		//$page =  ltrim($page,'/');
		//var_dump($page);
		//$url = 'https://api.weixin.qq.com/wxa/generatescheme?access_token='.\app\common\Wechat::access_token(aid,'wx');
		$url ='https://api.weixin.qq.com/wxa/genwxashortlink?access_token='.\app\common\Wechat::access_token(aid,'wx');
		$data = array();
		//$data['jump_wxa'] = ['path'=>$page,'query'=>$scene];
		//$data['is_expire'] = true;
		//$data['expire_time'] = time()+86400*30;
		$data['page_url'] = 'pages/shop/product?id='.$post['proid'].'&pid='.$this->member['id'];
		$data['page_title'] = $product['name'];
		$data['is_permanent']= false;
		$res = request_post($url,jsonEncode($data));
		//var_dump($res);
		$errmsg =json_decode($res,true);
		if($errmsg['errcode']){
			if($errmsg['errcode'] == -1){
				echojson(array('status'=>0,'msg'=>'系统繁忙，此时请稍候再试'));
			}else{
				echojson(array('status'=>0,'msg'=>$errmsg['errmsg']));
			}
		}
		
		return $this->json(['status'=>1,'openlink'=>$errmsg['link']]);
	}

    //商品评价【未购买也可以直接评价商品】
    public function commentProduct(){
        if(getcustom('product_comment')){
            $this->checklogin();
            $proid = input('param.proid/d', 0);
            $product = Db::name('shop_product')->where('aid', aid)->where('status', 1)->where('id', $proid)->find();
            if (empty($product)) {
                return $this->json(['status' => 0, 'msg' => '商品不存在']);
            }
            if(request()->isPost()) {
                $shopset = Db::name('shop_sysset')->where('aid', aid)->find();
                if ($shopset['product_comment'] == 0) return $this->json(['status' => 0, 'msg' => '评价功能未开启']);
                $score = input('param.score', 0);
                $content = input('param.content', '');
                $content_pic = input('param.content_pic', '');
                $needCheck = $shopset['product_comment_check'];
                $data['aid'] = aid;
                $data['mid'] = mid;
                $data['bid'] = $product['bid'];
                $data['proid'] = $product['id'];
                $data['orderid'] = 0;
                $data['ogid'] = 0;
                $data['ggname'] = '';
                $data['proname'] = $product['name'];
                $data['propic'] = $product['pic'];
                $data['score'] = $score;
                $data['content'] = $content;
                $data['openid'] = $this->member['openid'];
                $data['nickname'] = $this->member['nickname'];
                $data['headimg'] = $this->member['headimg'];
                $data['createtime'] = time();
                $data['content_pic'] = $content_pic;
                $data['status'] = ($needCheck == 1 ? 0 : 1);
                Db::name('shop_comment')->insert($data);
                if (!$needCheck) {
                    //不需要审核
                    $countnum = Db::name('shop_comment')->where('proid', $proid)->where('status', 1)->count();
                    $score = Db::name('shop_comment')->where('proid', $proid)->where('status', 1)->avg('score'); //平均评分
                    $haonum = Db::name('shop_comment')->where('proid', $proid)->where('status', 1)->where('score', '>', 3)->count(); //好评数
                    if ($countnum > 0) {
                        $haopercent = $haonum / $countnum * 100;
                    } else {
                        $haopercent = 100;
                    }
                    Db::name('shop_product')->where('id', $proid)->update(['comment_num' => $countnum, 'comment_score' => $score, 'comment_haopercent' => $haopercent]);
                }
                return $this->json(['status' => 1, 'msg' => '商品评价成功']);
            }
            return $this->json(['status' => 1, 'msg' => 'ok','product'=>$product]);
        }
        return $this->json(['status'=>0,'msg'=>'failed']);
    }

    public function checkDiscountCodeZc($invitecode = '')
    {
        if(getcustom('discount_code_zhongchuang')){
            //下单优惠代码 中创
            $invitecode = $invitecode ? $invitecode : input('param.discount_code_zc');
            if(empty($invitecode)){
                return $this->json(['status'=>0,'msg'=>'不能为空']);
            }
            $url ='https://zckl.zhoming.top/imcore/api/mall/getUserInfo';
            $data = array();
            $data['invitecode']= $invitecode;
            $res = curl_post($url,jsonEncode($data),0,array('Content-Type: application/json'));
            $res = json_decode($res,true);
            if($res['code'] == 200)
                return $this->json(['status' => 1, 'msg' => 'ok','data'=>$res['data']]);
            else
                return $this->json(['status' => 0, 'msg' => $res['msg'],'data'=>$res,'parama'=>$data]);
        }
    }
    public function getOrderAddress()
    {
        if(getcustom('member_auto_addlogin')){
			$address_info = Db::name('member_address')->where('aid',aid)->where('mid',mid)->where('isdefault',1)->find();
			if($address_info){
				$address = $address_info;
			}else{
				$address = [];
			}
			return $this->json(['status'=>1,'data'=>$address]);
		}
	}
    public function updataOrderAddress()
    {
        if(getcustom('member_auto_addlogin')){
			$address = input('post.');
			$post = $address;
			$orderid = intval($address['orderid']);
			
			$order = Db::name('shop_order')->where('id',$orderid)->where('aid',aid)->where('mid',mid)->find();
			if(!$order){
				return $this->json(['status'=>0,'msg'=>'没有找到订单']);
			}
			$area = explode(',',$address['area']);
			$address['province'] = $area[0];
			$address['city'] = $area[1];
			$address['district'] = $area[2];
			$address['area'] = implode('',$area);
            $orderdata['linkman'] = $address['name'];
            $orderdata['company'] = $address['company'];
			$orderdata['tel'] = $address['tel'];
			$orderdata['area'] = $address['area'];
			$orderdata['address'] = $address['address'];
			$orderdata['longitude'] = $address['longitude'];
			$orderdata['latitude'] = $address['latitude'];
			$orderdata['area2'] = $address['province'].','.$address['city'].','.$address['district'];
			$rs = Db::name('shop_order')->where('id',$orderid)->where('aid',aid)->where('mid',mid)->update($orderdata);

			$data = array();
			$data['aid'] = aid;
			$data['mid'] = mid;
			$data['name'] = $post['name'];
			$data['tel'] = $post['tel'];
			$data['address'] = $post['address'];
			$data['createtime'] = time();
            $data['company'] = $post['company'];
			$area = explode(',',$post['area']);
			$data['province'] = $area[0];
			$data['city'] = $area[1];
			$data['district'] = $area[2];
			$data['area'] = implode('',$area);
			$address_info = Db::name('member_address')->where('aid',aid)->where('mid',mid)->where('isdefault',1)->find();
			if($address_info){
				Db::name('member_address')->where('id',$address_info['id'])->update($data);
			}else{
				$data['isdefault'] = 1;
				Db::name('member_address')->insertGetId($data);
			}
            return $this->json(['status' => 1, 'msg' => 'ok','data'=>$order]);

        }
    }

}